我是一个完全的初学者,希望学习如何开始编码以构建我自己的应用程序。 我一直在摆弄经典的“Hello World”设计,并试图根据我在过去一周设法收集的有限知识对其进行改进,但我被卡住了,希望有人能帮助我!
到目前为止,我有一个供用户输入姓名的 UITextField、一个显示姓名的标签和一个更新显示的按钮。为了帮助更好地理解 UIAlertViews,如果输入的名称是 Chris(或 chris),我还包含一个警告,如果输入的名称不是 Chris,则包含另一个警告。到目前为止,所有这些都运行良好。
我想跟踪在我的 incorrectName UIAlertView 上按下了什么按钮,然后如果他们单击“我想被称为 Chris”按钮,则更新我的 UILabel 以说“Chris”。我之前从教程中构建了另一个与此类似的应用程序,并且复制了我认为应该具有相同功能但似乎不起作用的代码。
到目前为止,这是我的 .m 文件
#import "Solo1ViewController.h"
@interface Solo1ViewController ()
@end
@implementation Solo1ViewController
@synthesize setMessage;
@synthesize userName;
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonTitle=([alertView buttonTitleAtIndex:buttonIndex]);
if ([buttonTitle isEqualToString:@"I Want To Be Called Chris!"])
{setMessage.text=@"Chris";}
}
-(IBAction)showMessage:(id)sender
{
setMessage.text=userName.text;
if ([userName.text isEqual:@"Chris"] || [userName.text isEqual:@"chris"])
{
UIAlertView *correctName;
correctName = [[UIAlertView alloc]initWithTitle:@":)" message:@"Great Name!" delegate:nil cancelButtonTitle:@"Bye Fellow Chris" otherButtonTitles:nil, nil];
[correctName show];
}
else
{
UIAlertView *incorrectName;
incorrectName=[[UIAlertView alloc] initWithTitle:@":(" message:@"Chris is a Better Name" delegate:nil cancelButtonTitle:@"No, Thanks" otherButtonTitles:@"I Want To Be Called Chris!", nil];
[incorrectName show];
}
}
-(IBAction)removeKeyboard:(id)sender{
[userName resignFirstResponder];
}
对我来说,这似乎应该可行,但我想我可能遗漏了一些非常明显的东西,或者我做的完全错了。
最佳答案
确保将警报 View 的委托(delegate)设置为 self
!现在您已将委托(delegate)设置为 nil
。
例子:
incorrectName=[[UIAlertView alloc] initWithTitle:@":(" message:@"Chris is a Better Name" delegate:self cancelButtonTitle:@"No, Thanks" otherButtonTitles:@"I Want To Be Called Chris!", nil];
关于ios - 跟踪单击哪个 UIAlert 按钮以更改 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25354981/