如何在另一个函数中使用 Alertview 的 UITextField 值



我的Alertview中有一个UITextField。现在我想在另一个函数中使用此 UITextfield 值。只需在下面查看我的代码。

我的警报视图代码:

 -(void)AlertView 
{
UIAlertView *alert = [[UIAlertView alloc] init];
alert.title = @"Used Defualt Name Or you Can Save Recoring With Your Own Name";
alert.message = @"";
alert.delegate = self;
[alert addButtonWithTitle:@"Cancel"];
[alert addButtonWithTitle:@"OK"];   
myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 82.0, 260.0, 25.0)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
[alert release];
 }
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog(@"name:%@   buttonID:%d",myTextField.text,buttonIndex);
if (buttonIndex == 1) {
    btnPlay.enabled=YES;
    mySongname = myTextField.text;
     NSLog(@"song name:%@  ",mySongname);
}
else {
   btnPlay.enabled=YES;
}
}

我将变量 mysongname 声明为我的类的全局,因为我在这里使用它,我在下面的函数中有 alertview。

-(IBAction)playButton:(id)sender
{ 
 // mySongname=@"my fourite songs";
  NSString *fina = [NSString stringWithFormat:@"%@",mySongname];
}

现在在上面的函数中,我的歌曲名称显示超出范围的值,但是当我删除我在上面的函数中显示的注释时,它工作正常。我不知道为什么它不在这里通过 UITextfield。

尝试设置属性并合成为 mySongname,并使用 self.mySongname = myTextField.text 设置值;

只需声明一个字符串对象并设置其值并将其值用于类中的警报视图

最新更新