开发者

Can we pass a parameter to view did load or view will appear of other class from a class

Sorry if it is not a standard question, but now your solutions can help me out. In my app, I have two classes: ClassA and ClassB. ClassB has a table view in it. I am using a query to populate the table view in ClassB, 开发者_如何学Cbut the column name through which I have to query is what I get from ClassA, which I want to pass to ClassB and to perform the query and populate the table view. Can I pass a parameter to viewDidLoad or viewWillAppear? These get called when the view loads. Or is there any other method to do so?


Why don't you declare a property for that?

@interface ClassB:...

@property (nonatomic, copy) NSString * columnName

@end

and synthesize it in the implementation.

When you are creating ClassB instance from ClassA then do this,

ClassB * classB = [[ClassB alloc] initWithNibName:.. bundle:..];
classB.columnName = theColumnName;
[self pushViewController:classB animated:YES];
[classB release];

Make use of this parameter in viewDidLoad.


You normally create a property on the target class and populated it with whatever you need to pass along, then you push the other class onto the stack, or tell it to update.


I'd recommend you write a setter method for your query. Once Class B is instantiated you can call the method any time to update the data set and the table view.

You'll need to adjust the parameters to handle whatever form of representation you'll be using for your query.

ClassB.h

@interface ClassB : UITableViewController {

}

@property (nonatomic, copy) NSString *query;

ClassB.m

// method declared in ClassB

@implementation 

@synthesize query;

// other methods here ...

- (void)setQuery:(NSString *)newQuery
{   
    // query is an instance variable declared in your .h
    [newQuery retain];
    [query release];
    query = newQuery;

    // perform your new data fetch with the supplied query

    [self.tableView reload];
}

@end

ClassA.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    classB = [[ClassB alloc] initWithNibName:@"ClassB" bundle:nil];
    [classB setQuery:@"your query string here"];
}


You pass the value to your class or object constructor. You then save the value to a property in that class. You then access that property from the view did load, or your other events.

This is more a general programming concept, than anything related to objective c, the iPad, etc.

So, in your header file, you define a property. Then you synthesize that property the c file. Then you set the property in the constructor. Then you access the property in your view did load event.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜