Iphone 遍历obs的NS数组

Iphone 遍历obs的NS数组,iphone,objective-c,ios5,nsmutablearray,Iphone,Objective C,Ios5,Nsmutablearray,我需要使用这个数据结构,一个nsmutablearray,遍历每个索引并将每个字段打印到一个textfield中。每个“前进”按钮都会增加前面的数据,“后退”按钮会返回。我真的很难让它正常工作,有人知道我做错了什么吗。请记住,数组中包含的对象称为info_holder,它是一个包含3个字符串和一个计数器的对象: #import "ZDViewController.h" @implementation ZDViewController @synthesize password; @synthes

我需要使用这个数据结构,一个nsmutablearray,遍历每个索引并将每个字段打印到一个textfield中。每个“前进”按钮都会增加前面的数据,“后退”按钮会返回。我真的很难让它正常工作,有人知道我做错了什么吗。请记住,数组中包含的对象称为info_holder,它是一个包含3个字符串和一个计数器的对象:

#import "ZDViewController.h"

@implementation ZDViewController
@synthesize password;
@synthesize count;
@synthesize web;
@synthesize username;
@synthesize header;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSMutableArray *array = [[NSMutableArray alloc] init];
    info_holder *set1 = [[info_holder alloc] init];
    info_holder *set2 = [[info_holder alloc] init];
    info_holder *set3 = [[info_holder alloc] init];
    info_holder *set4 = [[info_holder alloc] init];
    info_holder *set5 = [[info_holder alloc] init];
    [set1 SetUser: @"info1"]; //temporary information until big is fixed
    [set1 SetPass: @"info2"];
    [set1 SetKey: @"webinfo1"];
    [set1 SetCount: 0];
    [set2 SetUser: @"info3"];
    [set2 SetPass: @"info4"];
    [set2 SetKey: @"webinfo2"];
    [set2 SetCount: 0];
    [set3 SetUser: @"info5"];
    [set3 SetPass: @"info6"];
    [set3 SetKey: @"webinfo3"];
    [set3 SetCount: 0];
    [set4 SetUser: @"info7"];
    [set4 SetPass: @"info8"];
    [set4 SetKey: @"webinfo4"];
    [set4 SetCount: 0];
    [set5 SetUser: @"info9"];
    [set5 SetPass: @"info10"];
    [set5 SetKey: @"Webinfo5"];
    [set5 SetCount: 0];

}

- (void)viewDidUnload
{
    [self setHeader:nil];
    [self setWeb:nil];
    [self setPassword:nil];
    [self setCount:nil];
    [self setUsername:nil];
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)increment:(id)sender {
    if(username.text==@"test")
        username.text=@"test2";
    else
        username.text=@"test";
    //this should print a new username NSSTRING, wedsite NSTRING, password NSTRING and INEGER counter, heald in the info_holder object, incremented per click backwards 
}

- (IBAction)decrement:(id)sender {

}
- (IBAction)Inc:(id)sender {

}
- (IBAction)textFieldDoneEditing:(id)sender {
    [sender resignFirstResponder];
}
- (IBAction)backgroundTap:(id)sender {
    [web resignFirstResponder];
    [username resignFirstResponder];
    [count resignFirstResponder];
    [password resignFirstResponder];
}
@end
我的主要观点是:

#import <UIKit/UIKit.h>

#import "ZDAppDelegate.h"
#import "info_holder.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {

        return UIApplicationMain(argc, argv, nil, NSStringFromClass([ZDAppDelegate class]));
    }
}
#导入
#导入“ZDAppDelegate.h”
#导入“info_holder.h”
int main(int argc,char*argv[])
{
@自动释放池{
返回UIApplicationMain(argc、argv、nil、NSStringFromClass([ZDAppDelegate类]);
}
}

首先,类名称应以大写开头,方法名称应以小写开头

第二,您的info_holder类(应该是InfoHolder)中的setter有潜在的问题

示例设置器应如下所示:

 - (void)setUser:(NSString *)username
{    
    [username retain];
    [uname release];
    uname=username;     
}
您没有说明什么不起作用,但一个问题是您没有保留这些字符串

基于评论 您需要将登录名声明为属性

ZDViewController.h

@interface ZDViewController : UIViewController

@property (strong, nonatomic) NSMutableArray *logins;
然后在
ZDViewController.m

@implementation ZDViewController

@synthesize logins;
.
.
.

- (void)viewDidLoad {
logins = [NSMutableArray array];
.
.
.
}
然后,您可以从类中的任何位置访问self.logins

-(iAction)forwards:(id)sender{
- (IBAction)forwards:(id)sender {
    //this should print a new username NSSTRING, wedsite NSTRING, password NSTRING and INEGER counter, heald in the info_holder object, incremented per click forward
for(int i=0;i<your_array.count;i++){
    username.text=[your_array objectAtIndex:i];
    password.text=[your_array objectAtIndex:i];
    key.text.text=[your_array objectAtIndex:i];


}
}
//这将打印一个新的用户名NSSTRING、wedsite NSSTRING、密码NSSTRING和Inger计数器,并将其合并到信息持有者对象中,每次向前单击递增
对于(int i=0;i您是否将您的info_holder对象存储到NSMutableArray实例中,如果是这样,那么您可以通过以下方法之一访问可变数组的元素(在您的情况下为info holder实例)
for(ClassName*tmp in _arrayName){tmp.property1;tmp.property2;…}

或者如果您希望它作为(int index=0;indexy您从来都不需要更改main.m。这正是问题所在,它给了我一个错误,告诉我您的_数组(在我的例子中称为logins)没有声明,但我确实声明了几行。另外,数组的索引I处的对象将有一个对象信息持有者,而信息持有者有3个文本字段(用户名、密码和密钥)和一个整数(计数器)问题是正确引用了数组中对象中的每个字符串。在上面的代码示例中,哪一行没有声明错误登录名?因为我看到您在
viewDidLoad
方法中声明登录名。除了该方法之外,您无法从任何地方访问登录名。如果您试图从f奥沃兹:方法,你需要使登录成为类的一个属性。这正是我需要帮助的地方。但我该怎么做呢?我是一个初学者,真的需要一些帮助查看我修改后的答案,然后阅读关于编程目标的苹果文档-C@AironZagarella:我进行所有树分配,然后增加I(在您的情况下,是指索引).据我从您的问题中了解,这是正确的。您需要访问类中属性上的变量(通过从项目中的不同类进行委派来访问)。(@dbajkovic也指出了一点).Apple docs很好,但CS193p视频讲座对我来说很好。只需谷歌搜索并观看iTunes就行了。虽然没有效果,但它帮助很大。我仍然无法从数组中的对象中获取每个字符串。谢谢,我会再等一段时间,然后检查最终答案。你需要更明确地说明哪些内容不起作用。你需要很可能你需要花更多的时间学习Objective-C,而不是来这里期望人们为你编写解决方案。很明显,你不了解Objective-C课程。如果你不花一点时间学习基础知识,你将不断迷失方向。