gpt4 book ai didi

iphone - 以编程方式添加 uibutton

转载 作者:行者123 更新时间:2023-12-03 20:23:22 24 4
gpt4 key购买 nike

这里有我正在尝试的代码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'
}

只是一个解释,openingHoursView.ohLocation 的 objectAtIndex:0 是“value1”,objectAtIndex:1 的解释是“value2”。打开HoursView.idNum 的过程相同

最佳答案

问题是你正在做:

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

self.view.backgroundColor = [UIColor whiteColor];

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

关于iphone - 以编程方式添加 uibutton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6200501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com