問題描述
我在將值傳遞給 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模板網!