問(wèn)題描述
我在將值傳遞給 destinationViewController 的 IBOutlet 屬性時(shí)遇到問(wèn)題,但它在普通屬性上工作正常,請(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.
感謝您的幫助.
推薦答案
我最近也遇到了同樣的問(wèn)題.但是當(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屬性并沒(méi)有在斷點(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 過(guò)渡期間,第二個(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)!