0

我的警报视图中有一个 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];
}

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

4

2 回答 2

2

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

于 2012-12-04T11:31:50.153 回答
0

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

于 2012-12-11T12:41:45.340 回答