I have an application which has a number of views which are navigated to via a number of custom UIButtons from my main view. The main view is named iBMRViewController and features some welcome graphics in the form of PNG images dropped in through interface builder. It also features 6 custom UIButtons which I have created through code using the following;
// This is the code which creates, and defines the properties of the 'Warning' button on the main view.
UIButton *warningButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
warningButton.frame = CGRectMake(225.0, 270.0, 60.0, 60.0);
[warningButton setTitle:@"" forState:UIControlStateNormal];
warningButton.backgroundColor = [UIColor clearColor];
[warningButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal ];
UIImage *warningButtonImageNormal = [UIImage imageNamed:@"Warning.png"];
UIImage *warningStretchableButtonImageNormal = [warningButtonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[warningButton setBackgroundImage:warningStretchableButtonImageNormal forState:UIControlStateNormal];
UIImage *warningButtonImagePressed = [UIImage imageNamed:@"whiteButton.png"];
UIImage *warningStretchableButtonImagePressed = [warningButtonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[warningButton setBackgroundImage:warningStretchableButtonImagePressed forState:UIControlStateHighlighted];
[warningButton addTarget:self action:@selector(warningButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self开发者_如何学C.view addSubview:warningButton];
Now, this all works fine and the buttons function perfectly, obviously I also have Actions set up for them which work perfectly. On each page I have a UINavigationBar with a UINavigationItem on set up through interface builder and set to take me back to my main view using the following code;
//This is the code which opens up the new view when 'Begin' button is tapped.
-(IBAction)beginHomeButtonAction:(id)sender {
iBMRViewController *controller = [[BeginView alloc] initWithNibName:@"iBMRViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
This also works, however, when it takes me back to the 'iBMRViewController' it ONLY displays what was set up via the interface builder xib file (i.e. the welcome png files). It does not display the buttons I added through code.
Can anyone give me an idea of where I have gone wrong? Would be extremely appreciated.
Thanks
In fact, the beginHomeButtonAction
doesn't "take you back to the main view". It creates a new view controller, with a new view, and presents that. It's the same kind of controller class, but not the same instance of that class.
To dismiss the view, and in fact take the user back to the main view, you have to dismiss the view that you presented from the main view. How to do this depends on how you presented then, but you could try popViewControllerAnimated
or dismissModalViewControllerAnimated
. (google is your friend)
精彩评论