TableOneViewController

TableOneViewController

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

这是我的示例:

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

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

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

我想使用TableViewController上的标签来管理TableOneViewController的NSArray。这可能吗?到目前为止,我的代码无法正常工作。

这是代码。谢谢!

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

最佳答案

问题在于,在呈现视图控制器之前尚未传递数组(尚未调用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上管理多个NSArray?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18519128/

10-09 12:45