Luai Kalkatawi
Luai Kalkatawi

Reputation: 1492

Hidden button action won't change issue

I'm trying to disable the navigation bar button when I start the app and after I finish the process(fetching data), I enable it back but unfortunately it won't enable.

Please where would be my issue? While I'm putting enable to YES and when I debug it I can see that it enabling it to 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;
   }
}

Upvotes: 0

Views: 93

Answers (3)

Burhanuddin Sunelwala
Burhanuddin Sunelwala

Reputation: 5343

Well there it is!

You are initializing your ViewController but that doesn't call your viewDidLoad: method. The viewDidLoad: method gets called when your ViewController is either Pushed or Presented! That is the time when the view gets loaded into the memory.

Therefore, the barButtons are never created and you are unable to see them.

So either make your network call inside the viewDidLoad: method of your ViewController

OR

Push the instance of your ViewController and then call the method unhide.

Edit

Since you are using Storyboards and not pushing any ViewController from AppDelegate, you need to use reference of your ViewController.

replace this in your - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method

ViewController *theInstance = (ViewController *)[(UINavigationController*)self.window.rootViewController topViewController]; 

Upvotes: 2

Paulw11
Paulw11

Reputation: 114783

You are calling [theInstance unhide] from the completion block of the AFHTTPOperation - this will almost certainly be executed on a background queue.

All UI operations must be performed on the main queue.

You should use -

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

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

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

        NSLog(@"Err");
    }];

Update

Your main problem is that theInstance points to an instance of your view controller that isn't on the screen - It is just an instance you have allocated but not actually presented.

Assuming this view controller is the initial view controller loaded by your application you can get a reference to the correct instance using [UIApplication sharedApplication].keyWindow.rootViewController

Upvotes: 1

Prajeet Shrestha
Prajeet Shrestha

Reputation: 8108

Remove this method from the completion block of app delegate.

 [theInstance unhide];

And add some delegate function which will be activated after the asynchronous call completes its task. And add that unhide method there (In your view controller may be).

Upvotes: 0

Related Questions