Reputation: 456
I have an asynchronous routine that fetches data from a server. When the refresh button in the navigation is tapped, I replace it with a UIActivityIndicatorView
, and that part works fine. The object that fetches the data has a protocol to inform its delegate when it has finished retrieving the data. When the delegate (UITableViewController
subclass) receives the method, I reload the table view with the new data and assign a new refresh button to the rightBarButtonItem
. When I do this, however, the UIBarButtonItem
doesn't change. This happens both when I call the reloadData
method before the assignment and when I call it after. I don't understand why the change just simply isn't happening. These are the two method calls I'm using.
This works:
UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicatorView.color = [UIColor orangeColor];
[indicatorView startAnimating];
[self.navigationItem.rightBarButtonItem initWithCustomView:indicatorView];
This is what doesn't work:
self.navigationItem.rightBarButtonItem initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshOrderList:)];
Am I supposed to change these barButtonItem
s in a different way?
Upvotes: 2
Views: 1385
Reputation: 8356
As mentioned above, you cannot change the values in the bar button item, particularly if it's a system item. If you desire, you can create the two items and have them available in ivars. It's not really that much execution time to create a new one though, especially if it may never be used.
This is being used in the case where on non-UITableViewController UIViewController has an outlet to the UITableView in question.
Swift 5
@IBOutlet var table: UITableView!
@IBAction func toggleTableEdit(sender:UIBarButtonItem) {
table.setEditing(!table.isEditing, animated: true)
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: (table.isEditing) ? .done : .edit, target: self, action: #selector(toggleTableEdit(sender:)))
}
Upvotes: 0
Reputation: 2441
Try this:
dispatch_async(dispatch_get_main_queue(), ^{
[self.navigationItem.rightBarButtonItem initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshOrderList:)];
});
Upvotes: 1
Reputation: 4168
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshOrderList:)];
You cannot initalise an already initialised object and expect it to work properly. Create a new bar button item and set it as the rightBarButtonItem
.
Upvotes: 3