简体   繁体   English

在视图之间传递数据不起作用

[英]Passing data between views isn't working

I've done this many times with code that is exactly the same, but for some reason it isn't working today. 我已经多次使用完全相同的代码完成了这项工作,但由于某种原因,它今天无效。

 ExampleViewController1 *exampleView = [[ExampleViewController1 alloc] initWithNibName:@"ExampleViewController1" bundle:nil];
 [exampleView setProjectName:[[self.projectListArray objectAtIndex:indexPath.row] objectForKey:@"name"]];
 NSLog(@"%@", [[self.projectListArray objectAtIndex:indexPath.row] objectForKey:@"name"]);
 XAppDelegate.stackController pushViewController:exampleView fromViewController:nil animated:YES]

My NSLog prints out appropriately. 我的NSLog打印得恰当。

My ExampleViewController1.h file declared like: My ExampleViewController1.h文件声明如下:

@property(nonatomic, strong) NSString *projectName; 

I then do this code in ExampleViewController1.m 's 然后我在ExampleViewController1.m执行此代码

 -(void)viewDidLoad {
     NSLog(@"%@", self.projectName);
     self.projectNameLabel.text = self.projectName;
     [super viewDidLoad];
 }

The results of my NSLog s are curious. 我的NSLog的结果很好奇。 The NSLog from my viewDidLoad appears to be getting called before my other one: 我的viewDidLoad中的NSLog似乎在我的另一个之前被调用:

 2012-04-22 10:59:41.462 StackedViewKit[43799:f803] (null)
 2012-04-22 10:59:41.463 StackedViewKit[43799:f803] NewTest

I have confirmed that the (null) value there is from NSLog(@"%@", self.projectName); 我已经确认了(null)值来自NSLog(@"%@", self.projectName); , but that should be the second NSLog called...I can't figure out why it is coming through first. ,但那应该是第二个被称为NSLog ...我无法弄清楚为什么它会先通过。

Someone requested this code: 有人要求这个代码:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
   if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {

    // random color
    self.view.backgroundColor = [UIColor colorWithRed:((float)rand())/RAND_MAX green:((float)rand())/RAND_MAX blue:((float)rand())/RAND_MAX alpha:1.0];
   }
  return self;
}

viewDidLoad is called before a view controller is displayed for the first time, not immediately after initWithNibName. viewDidLoad在第一次显示视图控制器之前调用,而不是在initWithNibName之后立即调用。

> viewDidLoad method is called after the view controller has loaded its view hierarchy into memory. > viewDidLoad方法在视图控制器将其视图层次结构加载到内存后调用。 This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView method. 无论视图层次结构是从nib文件加载还是在loadView方法中以编程方式创建,都会调用此方法。

> initWithNibName The nib file you specify is not loaded right away. > initWithNibName您指定的nib文件不会立即加载。 It is loaded the first time the view controller's view is accessed. 它是在第一次访问视图控制器的视图时加载的。 If you want to perform additional initialization after the nib file is loaded, override the viewDidLoad method and perform your tasks there. 如果要在加载nib文件后执行其他初始化,请覆盖viewDidLoad方法并在那里执行任务。

You can use App delegate to pass the data from one to another, that is another alternate solution. 您可以使用App委托将数据从一个传递到另一个,这是另一种替代解决方案。

you do in initWithNibName method itself. or in viewDidAppear.

Your initWithNibName method should be like this as per as @sch comments; 您的initWithNibName方法应该像@sch注释一样;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 

     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil] //just set it here first and then check

     if (self) { 
         // do something here; 
         } 
return self; 
}

We just need to be smart enough to think about what do we need to in constructor and what do we need to at viewDidLoad (once it had loaded into memory) 我们只需要足够聪明,思考构造函数中我们需要什么,以及viewDidLoad需要什么(一旦它加载到内存中)

As I expected, the problem is that you are trying to access self.view inside the initialization method. 正如我所料,问题是您正在尝试在初始化方法中访问self.view So move the line self.view.backgroundColor = ... to the viewDidLoad method: 所以将self.view.backgroundColor = ...行移到viewDidLoad方法:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"%@", self.projectName);
    self.projectNameLabel.text = self.projectName;
    self.view.backgroundColor = [UIColor colorWithRed:((float)rand())/RAND_MAX green:((float)rand())/RAND_MAX blue:((float)rand())/RAND_MAX alpha:1.0];
}

In fact, the documentation of the view property says: 事实上, view属性的文档说:

If you access this property and its value is currently nil, the view controller automatically calls the loadView method and returns the resulting view. 如果访问此属性并且其值当前为nil,则视图控制器会自动调用loadView方法并返回结果视图。

So when you call self.view in the initialization method, the view controller will have to load the view (from the nib or using the loadView method). 因此,当您在初始化方法中调用self.view时,视图控制器将必须加载视图(从nib或使用loadView方法)。 And that's why viewDidLoad is called. 这就是为什么调用viewDidLoad的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM