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

后端 未结 4 1261
悲&欢浪女
悲&欢浪女 2021-01-27 09:54

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

相关标签:
4条回答
  • 2021-01-27 10:10

    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 讨论(0)
  • 2021-01-27 10:25

    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.

    0 讨论(0)
  • 2021-01-27 10:28

    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.

    0 讨论(0)
  • 2021-01-27 10:33

    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"];
    }
    
    0 讨论(0)
提交回复
热议问题