【问题标题】:Is there a way to change a UIImageView's state from the App Delegate?有没有办法从 App Delegate 更改 UIImageView 的状态?
【发布时间】:2016-07-19 22:20:13
【问题描述】:

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

我想知道是否有从 App Delegate(从 Switch Case 内)向 ViewController 的 ViewDidLoad 方法发送消息,该方法将 UIImageView 设置为隐藏: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]; 同时可见。

如果不这样做,我可以尝试另一种方法来实现这一点吗?

`- (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];

应用委托中的 Switch 语句为:(包括 ViewController 引用)

-(void)playIndex:(NSInteger)index
{
    ViewController* mainController = (ViewController*)         self.window.rootViewController;

switch (index)
{
    case 1: [self playOne]; [mainController imageView1Hidden]; break;
    case 2: [self playTwo]; break;
    case 3: [self playThree]; break;
    case 4: [self playFour]; break;
}

}

【问题讨论】:

    标签: ios objective-c uiimageview switch-statement


    【解决方案1】:

    你可以这样做:

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

    【讨论】:

      【解决方案2】:

      这绝对是可能的,尽管您可能不应该在应用程序委托中包含此逻辑(但我承认我需要更多上下文来指导您)。

      您可以从应用委托访问您的视图控制器。有关如何做到这一点,请参阅此问题的答案:

      How do I access my viewController from my appDelegate? iOS

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

      @property BOOL imageView1Hidden;
      

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

      不完全优雅,但它会工作。

      【讨论】:

      • 非常感谢您的帮助。该页面的链接很有帮助,但它没有说明将MyViewController* mainController = (MyViewController*) self.window.rootViewController; 放在哪里我假设它进入我的-(void)playIndex:(NSInteger)index {
      • 当然。那是它可以去的一个地方。它可以去任何你需要引用你的视图控制器的地方。
      • 好的,有道理。我需要#import "ViewController.m" 吗?
      • 是的,您需要导入。但只需导入标题:#import "ViewController.h"
      • 哦,我明白了,难怪我得到链接器错误!在- (void)hideImageNO:(BOOL)imageView1Hidden 中,我似乎无法从ViewDidAppear 检索变量(我从ViewDidLoad 切换)我需要声明什么吗?
      猜你喜欢
      • 2011-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 2021-12-24
      相关资源
      最近更新 更多