I am having a problem with popToRootViewController. I have a root view A and a table view B. And when a row is selected in the table view I pass a string back to the root view and depending on the string. I change the title of buttons on A.
I have made a very simple version of this and put it on gitHub at this link: https://github.com/spennyf/didSelect_test. I am doing this because it is very hard to explain until you actually run this on your phone of the simulator. And see the flash. I don't know why it is happening or how to fix it. Any help would be greatly appreciated I will also post most of the code below, but if you could see the flash for yourself I think it would help explain the problem. Heres the code:
viewControllerA.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIButton *btn1;
@property (strong, nonatomic) IBOutlet UIButton *btn2;
@property (strong, nonatomic) IBOutlet NSString *object;
@end
viewControllerA.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated {
NSLog(@"object: %@", _object);
if ([_object isEqualToString:@"object1"]) {
[_btn1 setTitle:@"new1" forState:UIControlStateNormal];
}
if ([_object isEqualToString:@"object2"]) {
[_btn2 setTitle:@"new2" forState:UIControlStateNormal];
}
}
@end
tableviewB.m
#import "TableViewController.h"
#import "ViewController.h"
@interface TableViewController () {
NSMutableArray *_objects;
}
@end
@implementation TableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_objects = [[NSMutableArray alloc] init];
NSDictionary *obj1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"object1", @"title", nil];
NSDictionary *obj2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"object2", @"title", nil];
// NSDictionary *obj3 = [[NSDictionary alloc] initWithObjectsAndKeys:@"rpi", @"title", nil];
[_objects addObject:obj1];
[_objects addObject:obj2];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return _objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
//NSLog(@"%@", _objects);
cell.textLabel.text = [[_objects objectAtIndex:indexPath.row] objectForKey:@"title"];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ViewController *myController = (ViewController *)[self.navigationController.viewControllers objectAtIndex:0];
myController.object = [[_objects objectAtIndex:indexPath.row] objectForKey:@"title"];
[self.navigationController popToRootViewControllerAnimated:YES];
}
@end
Again thanks for the help.
There is no problem with your code. It is intended behavior for
UIButtons of typeUIButtonTypeSystemto flash when its title is changed.A simple workaround is to set the type of your buttons to
UIButtonTypeCustom.Edit: You can change the type of the button in Interface Builder: