有没有办法从App Delegate更改UIImageView的状态?

时间:2016-03-31 12:40:55

标签: ios objective-c uiimageview switch-statement

我在ViewController的ViewDidLoad中声明了4张图像,然后用myImage1.hidden = YES;隐藏它们。

我想知道是否还有从App Delegate(从Switch Case中)发送消息到ViewController的ViewDidLoad方法,该方法将UIImageView设置为隐藏:{{1} }

myImage1.hidden = NO;

我不确定这是可能的,但我想有人可能会对我正在尝试做的事情有所了解。我真的只是希望图像在-(void)playIndex:(NSInteger)index { switch (index) { case 1: [self playOne]; [// something here to tell the UIImageView .hidden = NO;] break; 被调用的同时变得可见。

如果没有这样做,还有其他方法可以尝试实现吗?

case 1: [self playOne];

app委托中的Switch语句是:(包括ViewController引用)

`- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

AppDelegate* app = [[UIApplication sharedApplication] delegate];


UIImage *redSquare = [UIImage imageNamed:@"red_square.png"];
UIImage *blueSquare = [UIImage imageNamed:@"blue_square.png"];
UIImage *greenSquare = [UIImage imageNamed:@"green_square.png"];
UIImage *yellowSquare = [UIImage imageNamed:@"yellow_square.png"];

UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(72, 117, 60, 60)];
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(188, 117, 60, 60)];
UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(72, 254, 60, 60)];
UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(188, 254, 60, 60)];

[self.view addSubview:imageView1];
[self.view addSubview:imageView2];
[self.view addSubview:imageView3];
[self.view addSubview:imageView4];

imageView1.animationImages = @[redSquare];
imageView2.animationImages = @[blueSquare];
imageView3.animationImages = @[greenSquare];
imageView4.animationImages = @[yellowSquare];

imageView1.animationDuration = 1;
imageView2.animationDuration = 1;
imageView3.animationDuration = 1;
imageView4.animationDuration = 1;

imageView1.hidden = YES;
imageView2.hidden = YES;
imageView3.hidden = YES;
imageView4.hidden = YES;

[imageView1 startAnimating];
[imageView2 startAnimating];
[imageView3 startAnimating];
[imageView4 startAnimating];

}

2 个答案:

答案 0 :(得分:1)

您可以通过以下方式执行此操作:

  1. 该ViewController的属性变量。
  2. 通过发送本地通知

答案 1 :(得分:0)

这绝对是可能的,虽然你可能不应该在app delegate中包含这个逻辑(但我承认我需要更多的上下文来指导你)。

您可以从应用代理访问您的视图控制器。请参阅此问题的答案,了解如何执行此操作:

How do I access my viewController from my appDelegate? iOS

然后,在视图控制器中,您可以拥有属性(BOOL类型),指定相应的图像视图是否应该可见。在视图控制器的.h文件中有类似的东西:

@property BOOL imageView1Hidden;

然后,在switch语句中设置该属性,并在viewDidLoad方法(视图控制器)中检查这些属性,以确定是否为图像视图设置隐藏状态。

不完全优雅,但它会起作用。