Uialertview 文本到 NSMutableArray,ios 4.3
我一直在尝试将文本输入从alertview文本字段复制到我稍后将使用的NSMutableArray,alertview弹出,我将输入输入到文本字段,但是当我按“确定”时,警报视图消失,但不将文本复制到我的可变数组,
这是我的 这里的代码
-(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 (
""
)
任何人都可以帮助该代码有什么问题吗?
i have been trying to copy text input from alertview textfield to a NSMutableArray that i will use later, alertview pops up and i enter the input to text field but when i press OK alert view disappears but doesnt copy text to my mutable array
here is my code
-(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);
}
}
here is my output on log screen
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 (
""
)
anyone can help whats wrong with that code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为当您将其添加到
[addCustomStand addObject:nameField.text];
中时,[nameField text]
还没有用户输入的值,因此将您的添加更改为数组
UIAlertView
委托方法。That's because
[nameField text]
doesn't have user entered value yet when you added it in your[addCustomStand addObject:nameField.text];
so change your adding into array in
UIAlertView
delegate method.在显示警报对话框之前,您需要将
nameField.text
添加到 addCustomStand 数组中。将其添加到数组时,该值是一个空字符串。相反,您需要在
clickedButtonAtIndex:
方法期间将值复制到数组中,方法如下:You are adding
nameField.text
to your addCustomStand array before you even show the alert dialog. At the time you add it to the array the value is an empty string.Instead you need to copy the value into your array during your
clickedButtonAtIndex:
method, by doing something like this: