Making an array of Objects in Objective-C.

前端 未结 2 683
暖寄归人
暖寄归人 2020-12-23 12:18

Been playing around with XCode for about 2 weeks now, and reading about MVC a bit. I have a problem trying to connect the Model to the Controller because I find it hard to g

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-23 13:04

    Here is an example that creates an NSMutableArray instance variable in the Controller class and adds a Person object to that array each time you evoke doSomeWork:

    @interface Controller
    
    NSMutableArray *personArray;
    
    @end
    @implementation Controller
    
    - (void) viewDidLoad {
        ................
        NSMutableArray *personsArray = [[NSMutableArray alloc] initWithCapacity:40];
    }
    
    - (IBAction)doSomeWork {
        Person *myPerson = [[Person alloc] init];
        myPerson.name = name;
        myPerson.age = age;
    
        [personsArray addObject:myPerson];
    }
    @end
    

提交回复
热议问题