通过initWithNibName传递的参数

通过initWithNibName传递的参数

本文介绍了通过initWithNibName传递的参数:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iPhone应用程序中当它从另一个viewcontroller类中的方法创建时,我需要将一些值传递给新的viewcontroller对象,以便我可以在新viewcontroller的(id)initWithNibName:method中初始化该值,然后将这些值加载到viewdidLoad方法中.

In iphone ApplicationI need to pass some values to a new viewcontroller object while it create from a method in another viewcontroller class so I can initialize that values in (id)initWithNibName:method of new viewcontroller then I can load those values in viewdidLoad method.

我想知道的是如何将值(参数)传递给新的viewcontrollor对象的构造函数(initWithNibName),例如java中的构造函数重载给我一些代码示例,仅显示如何使用附加参数调用initWithNibName以及如何在新创建的对象中检索它们谢谢...

what I want to know is how do I pass the values(parameters) to the constructor(initWithNibName) of a new viewcontrollor Object like constructor overloading in javagive me some code example just showing how initWithNibName called with extra parameters and how to retrieve them in the newly created objectThanks...

答案

这是我解决观察是具有属性的对象"问题的方式在ViewControllor.h中,我放了

this is the way I solve the problem "Observation is a object with attributes"in ViewControllor.h I put

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil set:(Observation *)observation;

在我放入的ViewControllor.m文件中

in ViewControllor.m file I put

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil set:(Observation *)observation{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization on passed parameter observation object
    draftObservation = observation;
}
return self;

}

然后我在另一堂课中这样称呼

then I call it this way in another class

 ObsevationListView *obsevationListView = [[ObservationViewControllor alloc]
                                          initWithNibName:@"ObservationViewControllor"
                                          bundle:nil set:observer];
[self.navigationController pushViewController:obsevationListView animated:YES];

它工作正常.如果有人从此获得帮助,我感到很高兴

it works fine. I'm glad if anyone get help from this

推荐答案

您应该在您的类中创建另一个初始化器,例如

You should create another initializer in your class, something like

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andParam:(id)aParam {
...
self.param = aParam;
}

这篇关于通过initWithNibName传递的参数:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:21