如何存储文本用户进入弹出文本字段到NSString?

时间:2014-08-15 18:50:02

标签: ios nsstring uitextfield uialertview

我的一些ViewController.h

NSString *title1;
NSString *sub1;
@interface MapMain : UIViewController
{
    IBOutlet UIButton *setter;
}

所有ViewController.m

-(IBAction)Write:(id)sender
{
    [self alertView];
}

-(void)alertView
{
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@""
                                                 message:@"Your Info"
                                                 delegate:self
                                                 cancelButtonTitle:@"Cancel"
                                                 otherButtonTitles:@"Done", nil];


    alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
    UITextField * alertTextField1 = [alert textFieldAtIndex:0];
    alertTextField1.keyboardType = UIKeyboardTypeDefault;
    alertTextField1.placeholder = @"Name";

    UITextField * alertTextField2 = [alert textFieldAtIndex:1];
    alertTextField2.keyboardType = UIKeyboardTypeDefault;
    alertTextField2.placeholder = @"Username";
    alertTextField2.secureTextEntry = NO;

    [alert show];
}

因此,当我点击特定按钮时,此代码正确地弹出一个带有两个文本字段的弹出窗口。如何在用户按下"完成"按钮进入我的NSString *title1NSString *sub1,以便我可以在以后按下其他按钮时使用字符串值。当用户按下"取消"以及如何使我的程序不存储文本。按钮?

1 个答案:

答案 0 :(得分:0)

让您的MapMain课程符合UIAlertViewDelegate协议

@interface MapMain : UIViewController <UIAlertViewDelegate>

然后实现代理的方法,以便何时解除警报

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) //only if cancel is not clicked (done is clicked)
    {
         UITextField * alertTextField1 = [alertView textFieldAtIndex:0];
         UITextField * alertTextField2 = [alertView textFieldAtIndex:1];
         //... do whatever else you need to here like
         title1 = alertTextField1.text;
    }
}