iPhone 中未调用 ViewDidLoad 方法?

发布于 2024-10-15 09:23:35 字数 815 浏览 1 评论 0原文

我已使用自定义 init 方法将 NSMutableDictionary 传递到 FirstClass 中。

第二类,

firstClass = [[FirstClass alloc] initWithFeedData:feedDictionary];

[self.navigationController pushViewController:firstClasss animated:YES];

第一类,

 - (FirstClass *) initWithFeedData:(NSMutableDictionary *) dataDictionary {
 {
     NSLog(@"%@", dataDictionary);

     [[NSBundle mainBundle] loadNibNamed:@"FirstClass" owner:self options:nil];

    //[self viewDidLoad]; Without call, ViewDidLoad method doesn't call.

 }

 - (void) viewDidLoad {

      [super viewDidLoad];

      NSLOG(@"Inside View DidLoad"); // This method never calls.
  }

在我的 viewDidLoad 方法中没有调用,如果我调用“[self viewDidLoad]”,那么只有 viewDidLoad 方法才能正常工作。我不知道为什么 viewDidLoad 方法不直接调用而不调用另一个方法?请帮帮我。

谢谢。

I have passed the NSMutableDictionary into the FirstClass using custom init method.

Second Class,

firstClass = [[FirstClass alloc] initWithFeedData:feedDictionary];

[self.navigationController pushViewController:firstClasss animated:YES];

FirstClass,

 - (FirstClass *) initWithFeedData:(NSMutableDictionary *) dataDictionary {
 {
     NSLog(@"%@", dataDictionary);

     [[NSBundle mainBundle] loadNibNamed:@"FirstClass" owner:self options:nil];

    //[self viewDidLoad]; Without call, ViewDidLoad method doesn't call.

 }

 - (void) viewDidLoad {

      [super viewDidLoad];

      NSLOG(@"Inside View DidLoad"); // This method never calls.
  }

In my viewDidLoad method doesn't called, if i call "[self viewDidLoad]" then only, viewDidLoad method works properly. I donno why viewDidLoad method doesn't call directly without calls in another method? Please Help me out.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫性小仙女 2024-10-22 09:23:35

如果您正在子类化 UIViewController,我认为您是因为您期待 viewDidLoad,那么您应该重写其指定的初始化程序 initWithNibName:bundle:< /code> 如果您使用 XIB,在这种情况下,viewDidLoad 将在 XIB 加载后调用。如果您不使用 XIB,则应实现 loadView 方法来创建视图,并且 viewDidLoad 将在 loadView 完成后调用。

If you're subclassing a UIViewController, which I assume you are since you're expecting viewDidLoad, then you should override its designated initialiser, initWithNibName:bundle: if you're using a XIB, in which case viewDidLoad will be called after the XIB loads. If you're not using a XIB, you should implement the loadView method to create your view, and viewDidLoad will be called after loadView finishes.

薄荷→糖丶微凉 2024-10-22 09:23:35

@Stephen是对的,至少你需要重写你的init语句以返回self。但是,在视图控制器上声明属性并以这种方式传递对象要更容易、更健壮。

FirstClass *firstClass = [[FirstClass alloc] initWithNibNamed:nil];
firstClass.feedDictionary = feedDictionary;

[self.navigationController pushViewController:firstClasss animated:YES];
[firstClass release];

您的 viewDidLoad 方法现在将被调用,您的 feedDictionary 将坐在那里等待您。

@Stephen is right, at the very least you need to rewrite your init statement to return self. However, it's much easier and more robust to declare a property on your view controller and pass objects in that manner.

FirstClass *firstClass = [[FirstClass alloc] initWithNibNamed:nil];
firstClass.feedDictionary = feedDictionary;

[self.navigationController pushViewController:firstClasss animated:YES];
[firstClass release];

Your viewDidLoad method will now be called and your feedDictionary will be sitting there, waiting for you.

花开浅夏 2024-10-22 09:23:35

您的 init 方法不正确。它应该看起来像这样:

- (id) initWithFeedData:(NSMutableDictionary *) dataDictionary {
  if (self = [super init]) {
    // TODO: do initialisation stuff with dataDictionary
  }

  return self;
}

假设 FirstClass 派生自 UIViewController

只要您的 NIB 文件与您的类具有相同的名称,它就应该自动加载 - 不需要 loadBundle: 调用。以下是文档的内容nibName 属性:

如果该属性的值为nil
并且您没有覆盖 loadView
自定义子类中的方法,
视图控制器寻找 nib 文件
谁的名字(不带 .nib
扩展名)与您的姓名相匹配
视图控制器类。

Your init method is not correct. It should look something like this:

- (id) initWithFeedData:(NSMutableDictionary *) dataDictionary {
  if (self = [super init]) {
    // TODO: do initialisation stuff with dataDictionary
  }

  return self;
}

This assumes that FirstClass is derived from UIViewController.

As long as your NIB file has the same name as your class it should be loaded in automatically -- there's no need for the loadBundle: call. Here's what the documentation says about the nibName property:

If the value of this property is nil
and you did not override the loadView
method in your custom subclass, the
view controller looks for a nib file
whose name (without the .nib
extension) matches the name of your
view controller class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文