Can some explain how to properly subclass a UIViewController and then load it into the window without using a nib.
Do i need to set the view and when do I do it? loadView?
My app crashes when I use my customUIViewController sub-class. Everything loads up find until I click a button and it compains about “Bad Access”
GettingStarted *vc = [[GettingStarted alloc] init];
UISplitViewController *split = self.splitViewController;
NSArray *vcArray = split.viewControllers;
NSLog(@"viewcontroll cout:%d", vcArray.count);
// this line of code crashes my app. I fi comment it out everything works fine
UINavigationController *detailViewController = (UINavigationController*)[vcArray objectAtIndex:1];
//
detailViewController pushViewController:vc animated:YES];
[vc release];
GettingStarted.m
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationItem setHidesBackButton:YES animated:YES];
//UIView *baseView = [[UIView alloc] init];
[[self navigationItem] setTitle:@"Getting Started"];
UIImageView *noUsersIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"no_users.png"]];
[noUsersIV setFrame:CGRectMake(0.0f, 0.0f, 466, 961.0f)];
[noUsersIV setHidden:NO];
[noUsersIV setTag:1006];
[[self view] addSubview:noUsersIV];
[noUsersIV release];
UIButton *btnAddUser = [UIButton buttonWithType:UIButtonTypeCustom];
[btnAddUser setImage:[UIImage imageNamed:@"no_users_button_standard.png"] forState:UIControlStateNormal];
[btnAddUser setImage:[UIImage imageNamed:@"no_users_button_activated.png"] forState:UIControlStateSelected];
[btnAddUser addTarget:self action:@selector(addUser:) forControlEvents:UIControlEventTouchUpInside];
[btnAddUser setFrame:CGRectMake(0, 428,466, 124)];
[[self view] addSubview:btnAddUser];
[btnAddUser release];
//self.view = baseView;
//[baseView release];
}
What is the result of your log message? Are you sure you have >=2 objects in that array?
Does the crash happen with pushNavigationController on the callstack, or does the crash happen later?
You also don’t need to release split, vcArray, or detailViewController. The [detailViewController release] will certainly cause a crash. The others would cause crashes later.
When functions return objects unless you are calling retain on it, or the function returning the object is named alloc/copy/create, the object will be autoreleased so you don’t need to release it.
The reason you would call retain in the second example above is if you wish to use the object outside of this function. If you do you need to retain it, if not it will be auto-released later.
Update: You have a similar problem where you are adding the button.