通过NS阵列的ob迭代

时间:2012-02-24 01:52:17

标签: iphone objective-c ios5 nsmutablearray

我需要使用这个数据结构,一个nsmutablearray,并遍历每个索引并将每个字段打印到文本字段中。每个“前进”按钮增加前面的数据,“后退”按钮返回。我很难让这个工作正常,有谁知道我做错了什么。请记住,数组中保存的对象(称为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]));
    }
}

3 个答案:

答案 0 :(得分:2)

- (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];


}
}

注意事项1:我假设您的texrfield名称为usernamepasswordkey

注意2 :你的问题太长了,我想没有人会读这么多代码来找到你的问题。只需分享您的需求。

答案 1 :(得分:1)

首先,类名应以Caps开头,方法名称应以小写字母开头。

其次,你在info_holder类中的setter(应该是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

答案 2 :(得分:0)

您是否将您的info_holder对象存储到NSMutableArray实例中,如果是,那么您可以通过以下任一方法访问可变数组的元素(在您的情况下为信息持有者实例),for(ClassName *tmp in _arrayName){ tmp.property1;tmp.property2;..... }
或者如果您希望它作为特定于索引的for(int index=0;index<[_arrayName count];index++){ ClassName *tmp = [_arrayName objectAtIndex:index];tmp.property1;tmp.property2;.....}
。我完全无法理解你的问题但是从你的着作中我猜你可以解决这个问题。如果没有让我知道你的问题,如果这肯定有助于你接受答案。
注意:正如@dbrajkovic建议的那样,始终遵循程序中的命名约定,这样可以提供更灵活的可读性。