Ios 访问alertController中声明的变量
我正在尝试创建一个提醒,提示用户命名他们导入到音乐表显示应用程序的歌曲 我已为此命名过程创建了一个函数:Ios 访问alertController中声明的变量,ios,objective-c,Ios,Objective C,我正在尝试创建一个提醒,提示用户命名他们导入到音乐表显示应用程序的歌曲 我已为此命名过程创建了一个函数: - (NSString *)nameImportedSong { NSString *songName; UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"New Song"
- (NSString *)nameImportedSong {
NSString *songName;
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"New Song"
message: @"Choose a song name"
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"song name";
textField.textColor = [UIColor blueColor];
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.borderStyle = UITextBorderStyleRoundedRect;
}];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSArray * textfields = alertController.textFields;
UITextField * namefield = textfields[0];
NSString *chosenSongName = [NSString stringWithFormat:@"%@", namefield.text];
}]];
songName = ; // <-------------how do I assign chosenSongName to songName?
[self presentViewController:alertController animated:YES completion:nil];
return songName;
}
-(NSString*)名称导入歌曲{
NSString*歌曲名;
UIAlertController*alertController=[UIAlertController alertControllerWithTitle:@“新歌”
信息:@“选择歌曲名称”
首选样式:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField*textField){
textField.placeholder=@“歌曲名称”;
textField.textColor=[UIColor blueColor];
textField.clearButtonMode=UITextFieldViewMode编辑时;
textField.borderStyle=UITextBorderStyleRoundedRect;
}];
[alertController addAction:[UIAlertAction actionWithTitle:@“确定”样式:UIAlertActionStyleDefault处理程序:^(UIAlertAction*操作){
NSArray*textfields=alertController.textfields;
UITextField*namefield=textfields[0];
NSString*chosenSongName=[NSString stringWithFormat:@“%@”,namefield.text];
}]];
songName=;//对变量使用_块关键字
__block NSString *chosenSongName;
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSArray * textfields = alertController.textFields;
UITextField * namefield = textfields[0];
chosenSongName = [NSString stringWithFormat:@"%@", namefield.text];
}]];
songName = chosenSongName;
NSLog(@"chosenSongName = %@",chosenSongName);
对变量使用_块关键字
__block NSString *chosenSongName;
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSArray * textfields = alertController.textFields;
UITextField * namefield = textfields[0];
chosenSongName = [NSString stringWithFormat:@"%@", namefield.text];
}]];
songName = chosenSongName;
NSLog(@"chosenSongName = %@",chosenSongName);
非常好,谢谢,非常好,谢谢。