xcode 当在 iphone 中单击按钮时,如何将文本字段/标签中的值添加到数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10476787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:12:26  来源:igfitidea点击:

How add values from text field / label to an array when a button click in iphone

iosxcodeipadnsmutablearraynsarray

提问by Ranga

I have a text field In that user can enter values from 1-10

我有一个文本字段,用户可以输入 1-10 之间的值

at the bottom of the text field i have button.

在文本字段的底部,我有按钮。

i need to add the values in text filed to array when user clicks done button

当用户单击完成按钮时,我需要将文本字段中的值添加到数组中

for this i wrote code like this.

为此,我编写了这样的代码。

- (void)viewDidLoad
{
    [super viewDidLoad];
    PainlevelsArray = [[NSMutableArray alloc] init]; //PainlevelsArray declared in .h

}


-(IBAction)doneClick:(id)sender
{
    [PainlevelsArray addObject:txtField.text ];

    // [PainlevelsArray insertObject:rateLabel.text atIndex:0 ];
    NSLog(@"PainlevelsArray *** %@",PainlevelsArray);

   [self.navigationController popToRootViewControllerAnimated:YES];

}

But every time array displays only the recently added one value only in log when button clicks, like this

但是每次当按钮点击时,数组只显示最近添加的一个值只在日志中,像这样

Output on Console

控制台输出

========================

========================

2012-05-07 10:11:15.689 ESPT[796:10d03] PainlevelsArray *** (
    2
)
2012-05-07 10:11:27.984 ESPT[796:10d03] PainlevelsArray *** (
    4
)

My array gets empty every time view loads, and adde a fresh entry when button clicksBut i need like this

My array gets empty every time view loads, and adde a fresh entry when button clicks但我需要这样

2012-05-07 10:11:27.984 ESPT[796:10d03] PainlevelsArray *** (
        4,
        5,
        2,
        6,
        8,
    )

All the velues enters are adde/inserted to the existing array.

所有进入的 veues 都添加/插入到现有数组中。

Thanks in advance

提前致谢

in ExerciseStartViewController

在运动开始视图控制器中

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 ExerciseResult *result = [[ExerciseResult alloc] init];
            [self.navigationController pushViewController:result animated:YES];
            [result release];
    }

in current/ ExerciseResult viewcontroller

在当前/运动结果视图控制器中

- (void)viewDidLoad
{
    [super viewDidLoad];
    PainlevelsArray = [[NSMutableArray alloc] init]; //PainlevelsArray declared in .h

}


-(IBAction)doneClick:(id)sender
{
    [PainlevelsArray addObject:txtField.text ];

    // [PainlevelsArray insertObject:rateLabel.text atIndex:0 ];
    NSLog(@"PainlevelsArray *** %@",PainlevelsArray);

   [self.navigationController popToRootViewControllerAnimated:YES];

}

回答by iPhone Developer

You need to use a global array.

您需要使用全局数组。

Best possible solution is declare an array in AppDelegate.h file and alloc-init this array in applicationdidfinishlaunch method.

最好的解决方案是在 AppDelegate.h 文件中声明一个数组,并在 applicationdidfinishlaunch 方法中分配初始化这个数组。

AppDelegate *app =  (AppDelegate*)[[UIApplication sharedApplication] delegate];
[app.arrMutable addObject:textField.text ]; 

Use UserDefaults: These are used to save your application data in key-value format. The values/objects stored as userdefaults always remain in application, until you have not removed it from sandbox. You can use following code to store an array in userdefaults.

使用 UserDefaults:这些用于以键值格式保存您的应用程序数据。作为用户默认值存储的值/对象始终保留在应用程序中,直到您尚未将其从沙箱中删除。您可以使用以下代码将数组存储在 userdefaults 中。

[[NSUserDefaults standardUserDefaults] setObject:app.arrMutable forKey:@"Global_Array_Key"];

In AppDelegae applicationDidFinishLuanch method you can retrieve this array from userdefaults using:

在 AppDelegae applicationDidFinishLunch 方法中,您可以使用以下方法从用户默认值中检索此数组:

self.arrMutable = [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults]objectForKey:@"Global_Array_Key"]];

回答by Ravi

Declare in Delegate.h page

在 Delegate.h 页面中声明

  @interface AppDelegate : NSObject <UIApplicationDelegate>
  {
       NSMutableArray *commString;
  }
  @property (nonatomic, retain) NSMutableArray *commString;
  @end

Delegate.m page

Delegate.m 页面

  @sythesize commString;

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
          commString = [[NSMutableArray alloc] init];
          // some code.....
 }

In your Button view

在您的按钮视图中

 -(IBAction)doneClick:(id)sender
{
NSMutableArray *mut=[[NSMutableArray alloc] initWithObjects:txtField.text,nil];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.commString=mut;

   // some code....
}

Declare this code where u want your array, you can easily get ur array.

在你想要数组的地方声明这段代码,你可以很容易地得到你的数组。

   AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
   NSLog(@"commString=%@",appDelegate.commString);

回答by KAREEM MAHAMMED

ExerciseResult *result = [[ExerciseResult alloc] init];
[self.navigationController pushViewController:result animated:YES];
[result release];

I think you might be fallow this type of code in you action for coming to this view. Instead of this you have to fallow to take as global ExerciseResult *result

我认为你可能会因为出现这种观点而在行动中放弃这种类型的代码。取而代之的是,您必须休耕以作为全球运动结果 *结果

in .h

在.h

ExerciseResult *result;

in .m

在.m

in viewDidLoad

result = [[ExerciseResult alloc] init];

in you action method you will fallow

在你的行动方法中,你会休耕

[self.navigationController pushViewController:result animated:YES];

you will follow like that

你会像那样跟随

I hope it will helps you

我希望它会帮助你

回答by alloc_iNit

You need to declare and initialize PainlevelsArray globally, i.e. in AppDelegate or separate class of shared instance, where memory has been assigned once and it won't over write its data even though any number of time you access the said class and so that it will continuously inserts new value into the array and you will get the desired result.

您需要全局声明和初始化 PainlevelsArray,即在 AppDelegate 或共享实例的单独类中,其中内存已分配一次,即使您访问该类的次数不限,它也不会覆盖其数据,因此它会不断向数组中插入新值,您将获得所需的结果。

回答by Dmorneault

You should use properties for an array in this situation.

在这种情况下,您应该使用数组的属性。

@property (nonatomic, strong) NSMutableArray *painlevelsArray;

Initialize the array

初始化数组

NSMutableArray *array = [[NSMutableArray alloc] init];
self.painlevelsArray = array;

Then add your string to the array after each button click

然后在每次单击按钮后将您的字符串添加到数组中

[self.painlevelsArray addObject:[NSString stringWithFormat:@"%@",self.textField.text]];

回答by vishiphone

You use this line of code when you click just write add this line.

当您单击 just write 添加此行时,您将使用这行代码。

[array addobject:textfield.text];

use this only.

只使用这个。

[PainlevelsArray addObject:txtField.text]