隐藏按钮操作不会改变问题

时间:2015-03-23 08:58:07

标签: ios objective-c uibutton

我在启动应用程序时尝试禁用导航栏按钮,在完成整个过程(获取数据)后,我启用了它,但遗憾的是它无法启用。

请问我的问题在哪里?虽然我将enable放到YES,但当我调试它时,我可以看到它启用YES

- (void)viewDidLoad {

    UIImage *searchBtn = [UIImage imageNamed:@"search_icon.png"];
    barButtonSearch = [[UIBarButtonItem alloc] initWithImage:[searchBtn imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(searchButton)];

    UIImage *menuBtn = [UIImage imageNamed:@"menu_icon.png"];
    barButtonMenu = [[UIBarButtonItem alloc] initWithImage:[menuBtn imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(menuButton)];

    self.navigationItem.rightBarButtonItem = barButtonMenu;
    self.navigationItem.leftBarButtonItem = barButtonSearch;

    barButtonMenu.enabled = NO;
    barButtonSearch.enabled = NO;
}

- (void)unhide{

    if (!(barButtonSearch.enabled && barButtonMenu.enabled)) {

        barButtonMenu.enabled = YES;
        barButtonSearch.enabled = YES;
    }
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    ViewController *theInstance = [[ViewController alloc] init];


    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        _dic  = (NSDictionary *)responseObject;

    [theInstance unhide];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"Err");
    }];

    [operation start];

    return YES;
   }
}

3 个答案:

答案 0 :(得分:2)

那就是它!

您正在初始化ViewController但不会调用viewDidLoad:方法。当您的ViewController被推送或呈现时,会调用viewDidLoad:方法!这是视图加载到内存中的时间。

因此,barButtons永远不会创建,您无法看到它们。

因此要么在ViewController的viewDidLoad:方法中进行网络调用

Push ViewController的实例,然后调用方法unhide

修改

由于您使用的是Storyboard而不是从AppDelegate推送任何ViewController,因此您需要使用ViewController的引用。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法

中替换它
ViewController *theInstance = (ViewController *)[(UINavigationController*)self.window.rootViewController topViewController]; 

答案 1 :(得分:1)

您正在从[theInstance unhide]的完成块调用AFHTTPOperation - 这几乎肯定会在后台队列中执行。

必须在主队列上执行所有UI操作。

你应该使用 -

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        _dic  = (NSDictionary *)responseObject;
        dispatch_async(dispatch_get_main_queue(),^{
            [theInstance unhide];
        });

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"Err");
    }];

更新

您的主要问题是theInstance指向您的视图控制器的一个实例,它不在屏幕上 - 它只是您已分配但未实际呈现的实例。

假设此视图控制器是您的应用程序加载的初始视图控制器,您可以使用[UIApplication sharedApplication].keyWindow.rootViewController

获取对正确实例的引用

答案 2 :(得分:0)

从app delegate的完成块中删除此方法。

 [theInstance unhide];

并添加一些委托函数,该函数将在异步调用完成其任务后激活。并在那里添加取消隐藏方法(在视图控制器中可能)。