是否可以在不同的视图控制器上管理多个NSArray?
这是我的例子:
我目前正在使用两个视图控制器:
TableViewController用作主菜单。我设置了多个UIButtons,每个都有一个标签,当用户按下一个按钮时,它会将它们带到TableOneViewController。
在TableOneViewController中,到目前为止我已经设置了UIImageView和UIButton,每次用户按下按钮时,UIImageView都会通过NSArray更改图像。
我想使用TableViewController上的标签来管理TableOneViewController的NSArrays。这可能吗?到目前为止,我的代码不起作用。
这是代码。谢谢!
表视图控制器.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];
}