1

是否可以在不同的视图控制器上管理多个NSArray

这是我的例子:

我目前正在使用两个视图控制器

TableViewController用作主菜单。我设置了多个UIButtons,每个都有一个标签,当用户按下一个按钮时,它会将它们带到TableOneViewController

TableOneViewController中,到目前为止我已经设置了UIImageViewUIButton,每次用户按下按钮时,UIImageView都会通过NSArray更改图像。

我想使用TableViewController上的标签来管理TableOneViewControllerNSArrays。这可能吗?到目前为止,我的代码不起作用。

这是代码。谢谢!

表视图控制器.m

- (IBAction)tableSelection:(id)sender
{

    TableOneViewController *TOVC = [self.storyboard instantiateViewControllerWithIdentifier:@"TableOneViewController"];


    if ([sender tag] == 1) {
        [[NSUserDefaults standardUserDefaults] setObject:TOVC.numOne forKey:@"num"];
        [[NSUserDefaults standardUserDefaults] setObject:@"1.png" forKey:@"num2"];

    }


    if ([sender tag] == 2) {
        [[NSUserDefaults standardUserDefaults] setObject:TOVC.numTwo forKey:@"num"];
        [[NSUserDefaults standardUserDefaults] setObject:@"2.png" forKey:@"num2"];

    }

    if ([sender tag] == 3) {
        [[NSUserDefaults standardUserDefaults] setObject:TOVC.numThree forKey:@"num"];
        [[NSUserDefaults standardUserDefaults] setObject:@"3.png" forKey:@"num2"];

    }


    [self presentViewController:TOVC animated:YES completion:nil];


}

TableOneViewController.m

- (IBAction)nextButton:(id)sender
{
    int index = tableCounter++ % [self.numOne count];
    self.num.image = self.numOne[index];
}

- (void)viewDidLoad
{

    self.num.image = [UIImage imageNamed:[[NSUserDefaults standardUserDefaults] objectForKey:@"num"]];
    self.num2.image = [UIImage imageNamed:[[NSUserDefaults standardUserDefaults] objectForKey:@"num2"]];




    [super viewDidLoad];

    tableCounter = 10;

    self.numOne = [NSArray arrayWithObjects:
                   [UIImage imageNamed:@"2.png"],
                   [UIImage imageNamed:@"3.png"],
                   [UIImage imageNamed:@"4.png"],
                   [UIImage imageNamed:@"5.png"],
                   [UIImage imageNamed:@"6.png"],
                   [UIImage imageNamed:@"7.png"],
                   [UIImage imageNamed:@"8.png"],
                   [UIImage imageNamed:@"9.png"],
                   [UIImage imageNamed:@"10.png"],
                   [UIImage imageNamed:@"1.png"],
                   nil];

    self.numTwo = [NSArray arrayWithObjects:
                   [UIImage imageNamed:@"20.png"],
                   [UIImage imageNamed:@"30.png"],
                   [UIImage imageNamed:@"40.png"],
                   [UIImage imageNamed:@"50.png"],
                   [UIImage imageNamed:@"60.png"],
                   [UIImage imageNamed:@"70.png"],
                   [UIImage imageNamed:@"80.png"],
                   [UIImage imageNamed:@"90.png"],
                   [UIImage imageNamed:@"100.png"],
                   [UIImage imageNamed:@"10.png"],
                   nil];

    self.numThree = [NSArray arrayWithObjects:
                   [UIImage imageNamed:@"200.png"],
                   [UIImage imageNamed:@"300.png"],
                   [UIImage imageNamed:@"400.png"],
                   [UIImage imageNamed:@"500.png"],
                   [UIImage imageNamed:@"600.png"],
                   [UIImage imageNamed:@"700.png"],
                   [UIImage imageNamed:@"800.png"],
                   [UIImage imageNamed:@"900.png"],
                   [UIImage imageNamed:@"1000.png"],
                   [UIImage imageNamed:@"100.png"],
                   nil];
}
4

2 回答 2

1

问题是您在呈现视图控制器(viewDidLoad尚未调用)之前传递数组,然后呈现它,但numOneviewDidLoad. 我建议使用延迟初始化,这里是 的示例numOne,其他两个数组也一样:

- (NSArray*) numOne
{
    if(!_numOne)
    {
       _numOne= [NSArray arrayWithObjects:
                   [UIImage imageNamed:@"2.png"],
                   [UIImage imageNamed:@"3.png"],
                   [UIImage imageNamed:@"4.png"],
                   [UIImage imageNamed:@"5.png"],
                   [UIImage imageNamed:@"6.png"],
                   [UIImage imageNamed:@"7.png"],
                   [UIImage imageNamed:@"8.png"],
                   [UIImage imageNamed:@"9.png"],
                   [UIImage imageNamed:@"10.png"],
                   [UIImage imageNamed:@"1.png"],
                   nil];
    }
    return _numOne;
}

然而,这不是 的目的NSUserDefaults,我认为这种方法是糟糕的设计。最好保留一个TableOneViewController指向三个数组之一的 ivar(图像名称相同)。

编辑

这就是我实现该机制的方式:一个附加属性,它指向以下三个数组之一TableOneViewController

@property (nonatomic, weak) NSArray* currentArray;

根据触发的发送者的标签,该指针应指向正确的数组tableSelection:

// TableViewController
- (IBAction)tableSelection:(id)sender
{

    TableOneViewController *TOVC = [self.storyboard instantiateViewControllerWithIdentifier:@"TableOneViewController"];
    switch([sender tag]) {
        case 1:
            TOVC.currentArray= TOVC.numOne;
            break;
        case 2:
            TOVC.currentArray= TOVC.numTwo;
            break;
        case 3:
            TOVC.currentArray= TOVC.numThree;
            break;
        default:
            return;
    }
    [self presentViewController:TOVC animated:YES completion:nil];
}

// TableOneViewController
- (IBAction)nextButton:(id)sender
{
    int index = tableCounter++ % [self.currentArray count];
    self.num.image = self.currentArray[index];
}
于 2013-08-29T19:52:43.897 回答
-3

它不起作用,因为

TableOneViewController *TOVC = [self.storyboard instantiateViewControllerWithIdentifier:@"TableOneViewController"];

创建一个全新的 TableOneViewController 实例。在我的程序执行期间,我会使用一个单例来保留数组和我需要的任何其他信息作为单个访问点。

于 2013-08-29T19:11:25.263 回答