gpt4 book ai didi

ios - 在不同的 UIViewController 上管理多个 NSArrays?

转载 作者:行者123 更新时间:2023-11-28 17:56:49 27 4
gpt4 key购买 nike

是否可以在不同的 View Controller 上管理多个 NSArrays

这是我的例子:

我目前正在使用两个 View Controller :

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

TableOneViewController 中,到目前为止,我已经设置了一个 UIImageView 和一个 UIButton,每次用户按下按钮时,< strong>UIImageView 通过 NSArray 更改图像。

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

这是代码。谢谢!

TableViewController.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];
}

最佳答案

问题是你在呈现 View Controller 之前传递了数组(viewDidLoad 还没有被调用),然后你呈现它,但是你初始化了 numOne(和其他数组)在 viewDidLoad 中。我建议使用惰性初始化,这里是 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];
}

关于ios - 在不同的 UIViewController 上管理多个 NSArrays?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18519128/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com