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

使用 prepareForSegue 方法時(shí),IBOutlet 屬性不會(huì)更新

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

問題描述

限時(shí)送ChatGPT賬號(hào)..

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

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

我已經(jīng)合成了這兩個(gè)屬性.

I already synthesize both the property.

感謝您的幫助.

推薦答案

我最近也遇到了同樣的問題.但是當(dāng)我一步步調(diào)試它時(shí),我找到了一個(gè)可能的原因.(對(duì)不起,我也是Objective C的新手,所以我下面的解釋可能不是那么準(zhǔn)確和專業(yè)......我之前的背景主要是Web開發(fā).)

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.)

如果你在你調(diào)用的那行之后設(shè)置一個(gè)斷點(diǎn)

If you set a breakpoint just after the line you call

testViewController *viewController = segue.destinationViewController;

當(dāng)你構(gòu)建并運(yùn)行項(xiàng)目時(shí),你會(huì)發(fā)現(xiàn)destinationViewController中的UITextField屬性并沒有在斷點(diǎn)處分配和啟動(dòng)(內(nèi)存為0x0).同時(shí) NSString 屬性已經(jīng)分配和初始化(所以你可以設(shè)置它的值).

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).

我認(rèn)為 UITextfield 可能是一個(gè)子視圖,因此它僅在其父視圖(目標(biāo)視圖)啟動(dòng)時(shí)才啟動(dòng).但是 NSString 是一個(gè)不與任何視圖關(guān)聯(lián)的屬性,因此它是由聲明它的視圖控制器分配和初始化的.

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.

當(dāng)我對(duì)此進(jìn)行進(jìn)一步測(cè)試時(shí),我發(fā)現(xiàn)了一件非常有趣的事情:第二個(gè)視圖是在 期間加載的- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender運(yùn)行.我做了如下測(cè)試代碼:

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:

在第一個(gè)視圖控制器.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 

}

在第二個(gè)視圖控制器.m文件中:

In the second view controller .m file:

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


    [super viewDidLoad];

}

注意到我在 NSLog 消息之前添加了序列號(hào).我發(fā)現(xiàn)最終的日志結(jié)果序列是 1,2,3,6,4,5 而不是 1,2,3,4,5,6.并且在日志3中,txtScene1結(jié)果為null(未啟動(dòng)),但是在日志4(第二個(gè)視圖加載完畢)之后,在日志5中,txtScene1結(jié)果為NOT null并且已經(jīng)啟動(dòng).這表明在 segue 執(zhí)行期間加載了第二個(gè)視圖.所以我猜想在 segue 過渡期間,第二個(gè)視圖控制器對(duì)象的啟動(dòng)順序是:第二個(gè)視圖控制器 -> NSString 屬性(以及其他類似的屬性,如 NSInteger 等) -> 第二個(gè)視圖 -> 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).

所以我在第一個(gè)視圖控制器 .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;
        }
    }

}

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

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

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

Hope above explanation is clear and helpful for you.

這篇關(guān)于使用 prepareForSegue 方法時(shí),IBOutlet 屬性不會(huì)更新的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

KIF: How to auto-run/stress test an iOS app to find the cause of a rare UI bug?(KIF:如何自動(dòng)運(yùn)行/壓力測(cè)試 iOS 應(yīng)用程序以找出罕見 UI 錯(cuò)誤的原因?)
Can#39;t change target membership visibility in Xcode 4.5(無法更改 Xcode 4.5 中的目標(biāo)成員身份可見性)
UITableView: Handle cell selection in a mixed cell table view static and dynamic cells(UITableView:在混合單元格表視圖靜態(tài)和動(dòng)態(tài)單元格中處理單元格選擇)
How to remove Address Bar in Safari in iOS?(如何在 iOS 中刪除 Safari 中的地址欄?)
iOS 5 SDK is gone after upgrade to Xcode 4.5(升級(jí)到 Xcode 4.5 后,iOS 5 SDK 消失了)
Having trouble creating UIImage from CIImage in iOS5(在 iOS5 中從 CIImage 創(chuàng)建 UIImage 時(shí)遇到問題)
主站蜘蛛池模板: 久久久天堂 | 欧美伦理一区二区 | 亚洲视频免费在线观看 | 久久精品99久久久久久 | 狠狠干狠狠操 | 久久888 | 日日不卡av | 国产成人午夜 | 成人免费毛片男人用品 | 在线免费观看黄 | 亚洲va韩国va欧美va精品 | av资源在线播放 | 又色又爽又黄gif动态图 | 免费一级a毛片夜夜看 | 亚洲不卡视频 | 酒色成人网 | 91精品91久久久中77777 | 午夜激情福利 | 国产欧美一区二区精品忘忧草 | 免费性网站 | 中文字幕麻豆 | 免费一级a毛片 | 狠狠干美女 | 色妞网 | 国产精品久久久久久久 | 小日子的在线观看免费第8集 | 亚洲777| 亚洲精品免费在线 | 成人h片在线观看 | a在线视频 | 国产午夜影院 | 国产一区二区三区 | 中文字幕在线观看网址 | 天天干天天舔 | 国内外成人免费视频 | 精品欧美一区二区精品久久 | 亚洲精品一区二区三区精华液 | 黄色三级免费 | 国产又色又爽又黄又免费 | 亚洲综合三区 | 96久久|