久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 prepareForSegue 方法時,IBOutlet 屬性不會更新

IBOutlet properties does not update when using prepareForSegue method(使用 prepareForSegue 方法時,IBOutlet 屬性不會更新)
本文介紹了使用 prepareForSegue 方法時,IBOutlet 屬性不會更新的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我在將值傳遞給 destinationViewController 的 IBOutlet 屬性時遇到問題,但它在普通屬性上工作正常,請參閱下面的代碼

I am having problem passing value to a IBOutlet property of destinationViewController but it works fine on ordinary property see code below

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"NewsCellToDetail"]) {        
    testViewController *viewController = segue.destinationViewController;
    viewController.titleLabel.text = @"test"; // set the IBOutlet label text to something
    NSLog(@"%@",viewController.titleLabel.text); // this will output to nil
    viewController.textTest = @"testing2"; // set the property to something
    NSLog(@"%@", viewController.textTest) // this will output the string testing2
}

這是頭文件testviewcontroller.h的代碼

This is the code for the header file testviewcontroller.h

#import <UIKit/UIKit.h>
@interface NewsDetailViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) NSString *textTest;
@end

我已經合成了這兩個屬性.

I already synthesize both the property.

感謝您的幫助.

推薦答案

我最近也遇到了同樣的問題.但是當我一步步調試它時,我找到了一個可能的原因.(對不起,我也是Objective C的新手,所以我下面的解釋可能不是那么準確和專業......我之前的背景主要是Web開發.)

I just got the same problem recently. But when I debug it step by step, I find a possible reason. (I'm sorry I'm also new to Objective C, so my following explanation may be not that accurate and professional ... My previous background is mainly web development.)

如果你在你調用的那行之后設置一個斷點

If you set a breakpoint just after the line you call

testViewController *viewController = segue.destinationViewController;

當你構建并運行項目時,你會發現destinationViewController中的UITextField屬性并沒有在斷點處分配和啟動(內存為0x0).同時 NSString 屬性已經分配和初始化(所以你可以設置它的值).

when you build and run the project, you will find that the UITextField property in the destinationViewController is not allocated and initiated (memory is 0x0) at the breakpoint. Meanwhile the NSString property is already allocated and initialised (so you can set its value).

我認為 UITextfield 可能是一個子視圖,因此它僅在其父視圖(目標視圖)啟動時才啟動.但是 NSString 是一個不與任何視圖關聯的屬性,因此它是由聲明它的視圖控制器分配和初始化的.

I think probably UITextfield is a child view, so it only initiates when its parent view (the destination view) is initiated. But NSString is a property that is not associated with any view, so it is allocated and initiated with the view controller which declares it.

當我對此進行進一步測試時,我發現了一件非常有趣的事情:第二個視圖是在 期間加載的- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender運行.我做了如下測試代碼:

When I do a further test of this, I find a very interesting thing: the second view is loaded during the - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender runs. I do a test code like below:

在第一個視圖控制器.m文件中:

In the first view controller .m file:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSLog(@"1. %@, %@",[segue identifier],segue.destinationViewController);

    Scene2Controller *scene2ViewController = [segue destinationViewController];
    [txtScene2 resignFirstResponder];

    NSLog(@"2. scene2ViewController: %@", scene2ViewController);
    NSLog(@"3. txtScene1: %@; passValue: %@", [scene2ViewController txtScene1], scene2ViewController.passValue);
    NSLog(@"4. View2: %@; passValue: %@", [scene2ViewController view], scene2ViewController.passValue);
    NSLog(@"5. txtScene1: %@; passValue: %@", [scene2ViewController txtScene1], scene2ViewController.passValue);

    //txtScene1 is the UITextfield property I declared in the second view controller
    //txtScene2 is the UITextfield property I declared in the first view controller
    //passValue is the NSString property I declared in the second view controller 

}

在第二個視圖控制器.m文件中:

In the second view controller .m file:

- (void)viewDidLoad
{
    NSLog(@"6. txtScene1: %@; passValue: %@", txtScene1,passValue);


    [super viewDidLoad];

}

注意到我在 NSLog 消息之前添加了序列號.我發現最終的日志結果序列是 1,2,3,6,4,5 而不是 1,2,3,4,5,6.并且在日志3中,txtScene1結果為null(未啟動),但是在日志4(第二個視圖加載完畢)之后,在日志5中,txtScene1結果為NOT null并且已經啟動.這表明在 segue 執行期間加載了第二個視圖.所以我猜想在 segue 過渡期間,第二個視圖控制器對象的啟動順序是:第二個視圖控制器 -> NSString 屬性(以及其他類似的屬性,如 NSInteger 等) -> 第二個視圖 -> UITextfield 屬性(和其他子視圖屬性).

Noticed that I add sequential numbers before NSLog messages. I found the final log result sequence was 1,2,3,6,4,5 rather than 1,2,3,4,5,6. And in log 3, the txtScene1 result was null (not initiated), but after log 4 (the second view was loaded), in log 5, the txtScene1 was NOT null and has been initiated. This suggested that the second view was loaded during the segue performs. So I guess that during the segue transition, the initiation sequence of the second view controller objects would be: second view controller -> NSString property (and other similar properties, like NSInteger, etc.) -> second view -> UITextfield property (and other subview properties).

所以我在第一個視圖控制器 .m 文件中更改了我的代碼,如下所示:

So I changed my codes in the first view controller .m file as below:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    Scene2Controller *scene2ViewController = [segue destinationViewController];
    [txtScene2 resignFirstResponder];

    if ([scene2ViewController view]) {
        if ([txtScene2.text isEqualToString:@""]) {
            scene2ViewController.txtScene1.text = @"No Value";
        } else {
            scene2ViewController.txtScene1.text = txtScene2.text;
        }
    }

}

然后這段代碼可以正常工作,并且值會直接傳遞給第二個視圖中的 UITextfield 屬性.

This code then works fine and the value is passed directly to the UITextfield property in the second view.

希望上面的解釋清楚,對你有幫助.

Hope above explanation is clear and helpful for you.

這篇關于使用 prepareForSegue 方法時,IBOutlet 屬性不會更新的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

KIF: How to auto-run/stress test an iOS app to find the cause of a rare UI bug?(KIF:如何自動運行/壓力測試 iOS 應用程序以找出罕見 UI 錯誤的原因?)
Can#39;t change target membership visibility in Xcode 4.5(無法更改 Xcode 4.5 中的目標成員身份可見性)
UITableView: Handle cell selection in a mixed cell table view static and dynamic cells(UITableView:在混合單元格表視圖靜態和動態單元格中處理單元格選擇)
How to remove Address Bar in Safari in iOS?(如何在 iOS 中刪除 Safari 中的地址欄?)
iOS 5 SDK is gone after upgrade to Xcode 4.5(升級到 Xcode 4.5 后,iOS 5 SDK 消失了)
Having trouble creating UIImage from CIImage in iOS5(在 iOS5 中從 CIImage 創建 UIImage 時遇到問題)
主站蜘蛛池模板: 国产精品亚洲视频 | 欧美在线a | 久久久久国产成人精品亚洲午夜 | 亚洲精品国产精品国自产在线 | 成人高清网站 | 黄色片大全在线观看 | 国产综合精品一区二区三区 | 黄色中文字幕 | 国产综合一区二区 | 日韩免费在线观看视频 | 久久国产精品一区二区 | 亚洲人成人一区二区在线观看 | avav在线看 | 夜夜骚视频| 国产黄色小视频 | 日韩乱码一二三 | 精品国产99 | 国产中文字幕网 | 精品欧美一区二区在线观看欧美熟 | 午夜影院免费体验区 | 国产在线一区观看 | 精品毛片 | 午夜欧美一区二区三区在线播放 | 精久久久 | 精品一二三区 | 精品久久国产 | 人人艹人人爽 | 少妇性l交大片免费一 | 成人免费淫片aa视频免费 | 97精品国产97久久久久久免费 | 午夜精品一区二区三区在线播放 | 天天干天天谢 | 欧美成人一区二区三区 | 欧美成人免费在线视频 | 尤物在线精品视频 | 国产精品免费一区二区三区四区 | 日韩免费高清视频 | 成人在线看片 | 国产精品久久久久久久久免费桃花 | 香蕉视频久久久 | 国产香蕉视频 |