Iphone 以编程方式添加uibuttons
这里我有这个代码,我正在哪里尝试Iphone 以编程方式添加uibuttons,iphone,uibutton,Iphone,Uibutton,这里我有这个代码,我正在哪里尝试 openingHoursView.ohLocation有两个值“value1”和“value2”。 使用值“id1”和“id2”的openingHoursView.idNum也是如此。 当我执行代码时,我只得到一个名为“value2”和“id2”的按钮。所以我的问题是如何为openingHoursView.ohLocation和openingHoursView.idNum - (void)loadView { storeAppDelegate = (S
openingHoursView.ohLocation
有两个值“value1”和“value2”。
使用值“id1”和“id2”的openingHoursView.idNum
也是如此。
当我执行代码时,我只得到一个名为“value2”和“id2”的按钮。所以我的问题是如何为openingHoursView.ohLocation
和openingHoursView.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'
}
-(无效)加载视图{
storeAppDelegate=(storeAppDelegate*)[[UIApplication sharedApplication]委托];
int row=[storeAppDelegate.openingHoursLocationsDelegate计数]-1;
对于(int i=0;i而言,问题在于您正在执行:
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor whiteColor];
在for
循环中,因此在每次迭代中都会被覆盖。在for
循环之前,您应该只执行一次。您正在为中的每个按钮创建一个新视图
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
将此代码放在for循环之前。UIButton
*button
=[[UIButton
alloc
]init
];
[self
view
addsubView:按钮]