在这里,我有此代码,我正在尝试
openingHoursView.ohLocation具有两个值“ value1”和“ value2”。
openingHoursView.idNum的值“ id1”和“ id2”也是如此。
当我执行代码时,我只会得到一个名为“ value2”和“ id2”的按钮。所以我的问题是如何为openingHoursView.ohLocationopeningHoursView.idNum的值创建所有按钮

- (void)loadView {

    storeAppDelegate = (StoreAppDelegate *)[[UIApplication sharedApplication] delegate];

    int row = [storeAppDelegate.openingHoursLocationsDelegate count]-1;
    for (int i = 0; i <= row; i++) {
        openingHoursView = [storeAppDelegate.openingHoursLocationsDelegate objectAtIndex:i];

        self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

        self.view.backgroundColor = [UIColor whiteColor];

        //create the button
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        //set the position of the button
        if (i%2 != 0) {
            button.frame = CGRectMake(30 , 100 , 150, 30);

        }else {
            button.frame = CGRectMake(30 , 100 + i*50, 150, 30);
        }


        //set the button's title
        [button setTitle:openingHoursView.ohLocation forState:UIControlStateNormal];

        //listen for clicks
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = [openingHoursView.idNum intValue];
        //add the button to the view
        [self.view addSubview:button];
    }
}

-(IBAction) buttonPressed: (id) sender {
    UIButton* button = (UIButton*)sender;

    NSLog(@"User clicked %d", button.tag);
    // Do something here with the variable 'sender'
}


对于openHoursView.ohLocation的解释objectAtIndex:0是“ value1”,对于objectAtIndex:1的解释是“ value2”。 openHoursView.idNum的相同过程

最佳答案

问题是您在做:

self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

self.view.backgroundColor = [UIColor whiteColor];


for循环中,因此每次迭代都会被覆盖。您只能在for循环之前执行一次。

10-08 12:28