Uialertview文本到NSMutableArray,ios 4.3
我一直试图将文本输入从alertview文本字段复制到我稍后将使用的NSMutableArray,alertview弹出,我输入文本字段的输入,但当我按OK时,alert view消失,但不会将文本复制到我的可变数组 这是我的密码Uialertview文本到NSMutableArray,ios 4.3,ios,nsmutablearray,uialertview,Ios,Nsmutablearray,Uialertview,我一直试图将文本输入从alertview文本字段复制到我稍后将使用的NSMutableArray,alertview弹出,我输入文本字段的输入,但当我按OK时,alert view消失,但不会将文本复制到我的可变数组 这是我的密码 -(IBAction)add:(UIButton *)sender { addCustomStand = [[NSMutableArray alloc] init]; UIAlertView* dialog = [[UIAlertView alloc]
-(IBAction)add:(UIButton *)sender
{
addCustomStand = [[NSMutableArray alloc] init];
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"Enter a Stand Location"
message:@" "
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
UITextField *nameField = [[UITextField alloc]
initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
nameField.text = @"";
[dialog addSubview:nameField];
if ([nameField text]){
NSLog(@"Name Field %@ ",nameField.text);
[addCustomStand addObject:nameField.text];
}
[nameField release];
[dialog show];
[dialog release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"OK"])
{
NSLog(@"Button 1 was selected.");
NSLog(@"StandLocations %@ ",addCustomStand);
}
}
这是我在日志屏幕上的输出
2012-02-07 20:26:57.315 Avicii[1399:b603] Name Field
2012-02-07 20:26:59.720 Avicii[1399:b603] Button 1 was selected.
2012-02-07 20:26:59.721 Avicii[1399:b603] StandLocations (
""
)
任何人都能帮上忙那个代码有什么问题吗 在显示警报对话框之前,您正在将nameField.text添加到addCustomStand数组中。将其添加到数组时,该值为空字符串
相反,您需要在ClickedButtonIndex:方法期间将值复制到数组中,方法如下:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"OK"])
{
NSString *location;
UIView *view;
for (view in [alertView subviews]) {
if ([view isKindOfClass:[UITextField class]]) {
location = [(UITextField*)view text];
}
}
if (location) {
[addCustomStand addObject:location];
}
}
}
这是因为当您将[nameField text]添加到[addCustomStand addObject:nameField.text]中时,它还没有用户输入的值
因此,在UIAlertView委托方法中更改添加到数组中的内容
-(IBAction)add:(UIButton *)sender
{
addCustomStand = [[NSMutableArray alloc] init];
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"Enter a Stand Location"
message:@" "
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
UITextField *nameField = [[UITextField alloc]
initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
nameField.text = @"";
// Note at this line
nameField.tag = 100;
//
[dialog addSubview:nameField];
[nameField release];
[dialog show];
[dialog release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"OK"])
{
// Note at this line
UITextField* nameField = (UITextField *)[alertView viewWithTag:100];
[addCustomStand addObject:nameField.text];
//
NSLog(@"Button 1 was selected.");
NSLog(@"StandLocations %@ ",addCustomStand);
}
}
感谢您的帮助,nameField.tag=100;行吗?那只是给你的名字字段加上一个标签,它可以是任何数字,我只是随意选择100。它的作用是在将名称字段添加到子视图之前,为其指定一个标记号100。因此,在UIAlertView委托中,可以使用[alertView viewWithTag:100]检索那里的名称字段