我有一个警报视图设置为两个名称输入,如此
UITextField * player1; UITextField * player2;
UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:@"Enter 2 player names"
message:@"\n\n\n" // IMPORTANT
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
player1 = [[UITextField alloc] initWithFrame:CGRectMake(12, 50, 260, 25)];
[player1 setBackgroundColor:[UIColor whiteColor]];
[player1 setPlaceholder:@"player1"];
[prompt addSubview:player1];
player2 = [[UITextField alloc] initWithFrame:CGRectMake(12, 85, 260, 25)];
[player2 setBackgroundColor:[UIColor whiteColor]];
[player2 setPlaceholder:@"player2"];
[prompt addSubview:player2];
// set place
[prompt setTransform:CGAffineTransformMakeTranslation(0, 110)];
[prompt show];
//[prompt release];
// set cursor and show keyboard
[player1 becomeFirstResponder];
现在我想处理“确定”按钮点击。我试图做这样的事情没有运气..
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
{
NSLog(@"cancel");
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OK works" message:@"no error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
据我所知,这应该有效。但是当我按下“确定”按钮时没有任何反应。
答案 0 :(得分:5)
您需要为self
设置UIAlertView
代理。
UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:@"Enter 2 player names"
message:@"\n\n\n" // IMPORTANT
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
答案 1 :(得分:1)
注意: 不相关的原始问题,但根据评论“你能否给我一些更详细的信息,你的意思是将标签分配给textfield,然后获取textfield& its价值?“我在这里发帖答案。
将标签分配给文本字段
UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:@"Enter 2 player names"
message:@"\n\n\n" // IMPORTANT
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
player1 = [[UITextField alloc] initWithFrame:CGRectMake(12, 50, 260, 25)];
[player1 setBackgroundColor:[UIColor whiteColor]];
[player1 setPlaceholder:@"player1"];
[player1 setTag:100]; // added this
[prompt addSubview:player1];
player2 = [[UITextField alloc] initWithFrame:CGRectMake(12, 85, 260, 25)];
[player2 setBackgroundColor:[UIColor whiteColor]];
[player2 setPlaceholder:@"player2"];
[player2 setTag:200]; // added this
[prompt addSubview:player2];
// set place
[prompt setTransform:CGAffineTransformMakeTranslation(0, 110)];
[prompt show];
//[prompt release];
// set cursor and show keyboard
[player1 becomeFirstResponder];
检索值
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(@"cancel");
}
else
{
UITextField *txtPlayer1 = (UITextField*)[alertView viewWithTag:100];
NSLog(@"Value for player1: %@",txtPlayer1.text);
UITextField *txtPlayer2 = (UITextField*)[alertView viewWithTag:200];
NSLog(@"Value for player2: %@",txtPlayer2.text);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OK works" message:@"no error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
答案 2 :(得分:0)
我使用以下代码检查uialertview的按钮索引。
如上所述,在显示UIAlertview时也将代表设置为自己,然后......
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
NSLog(@"index 0 pressed ie cancel button");
// do something when cancel pressed
}
else NSLog(@"index 1 pressed ie ok button");
// do something when ok button pressed
}