rightbarbutton not animating properly - iphone

I am animating to a view with it sliding from right to left. The left bar button looks like it is in the navigation but the rightbarbutton seems to animated weirdly, almost like at first its not part of the navigation bar.
I am wanting to know if there is something wrong in my code... or if there is another way of doing this?
- (void)viewWillAppear:(BOOL)animated
{
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Skip" style:UIBarButtonItemStyleBordered target:nil action:nil];
[super viewWillAppear:animated];
}
Any help would be greatly appreciated.

Set rightBarButtonItem in the view controller's init... method. It only has to be done once in the view controller's lifetime.

This is just a shot in the dark, but the convention is to first call [super viewWillAppear:animated], then add your own code. Do that whenever you're constructing things (objects, views, etc).
Then when tearing things down (-dealloc, -viewWillDisappear, etc.), do the opposite. Clean up your stuff, then call the super to let it do its cleanup.
That probably isn't related and won't make a difference. But it's a good habit to get in to, and you never know. It might change the behavior here. Maybe the super needs to do do some setup before you add the bar button item.

Related

How can I hide a UIBarButtonItem?

I have created a simple UI in IB, this consists of a UINavigationBar and a UIBarButtonItem that I dragged and dropped on the right hand side.
I am trying to set this button to be hidden a certain times but I am having some problems.
So far I have tried using:
self.NavigationItem.rightBarButton = nil;
...which didn't work for me. I have also tried creating and IBOutlet and linking it to the button however I'm having problems with this too. I think it should be pretty simple and maybe I'm over-complicating it, but at this point I'm pretty stumped!
Please can someone help me out?
UINavigationItem doesnt have a rightBarButton property. Try rightBarButtonItem instead (or [self.navigationItem setRightBarButtonItem:nil animated:NO];):
self.navigationController.navigationItem.rightBarButtonItem = nil;
// Or
self.navigationItem.rightBarButtonItem = nil;
// Or
[self.navigationItem setRightBarButtonItem:nil animated:NO];
Just reset the buttons
-(void)setItems:(NSArray *)items animated:(BOOL)animated
More info here: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIToolbar_Class/Reference/Reference.html#//apple_ref/occ/instm/UIToolbar/setItems%3aanimated%3a
You can get the current items using the items property, then just remove the one you don't want to show and pass in the new NSArray.
You can also add a UIButton as the UIBarButtonItem's customView. Then set the hidden property on the customView (UIButton)
Rather than deleting the bar button item and destroying the button and it's attached storyboard segue, you can just set it to clear text when it's disabled.
[self.navigationItem.rightBarButtonItem setTitleTextAttributes:#{NSForegroundColorAttributeName:[UIColor clearColor]}
forState:UIControlStateDisabled];
Then when ever you want the bar button item hidden, you can just do:
self.navigationItem.rightBarButton.enabled = NO;
It's lame there's no hidden property but this offers the same result.
Actually, you can just create an IBOutlet reference to the desired UIBarButtonItem and when needed just do as follow:
[self.yourOutletRerence setImage: nil];
The simplest solution: Just change the BarButtonItem's identifier to custom.

iOS: confused about using the AppDelegate properly

Still really new to iOS dev (have a background in asp.net / web), and I don't think I've quite gotten my head around how everything relates to everything else. For example, I'm building an app at the moment which starts with a NavigationController. I'm passing ViewControllers in and out of that quite happily and everything is working but now I need to add a rightbutton to the navigation controller. I have done this from within one of the ViewControllers like this:
- (void)viewDidLoad
{
UIBarButtonItem *change = [[UIBarButtonItem alloc] initWithTitle:#"CHANGE" style:UIBarButtonItemStylePlain target:self action:#selector(navAlert)];
self.navigationItem.rightBarButtonItem = change;
[change release];
[super viewDidLoad];
}
- (void)navAlert
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"nav pressed" message:#"You pressed the Change button" delegate:nil cancelButtonTitle:#"Yep" otherButtonTitles:nil];
[alert show];
[alert release];
}
but I don't want to have to do that in each controller - should something like this go into the AppDelegate and be called from there?
Also, suppose I didn't want to start with a NavigationController - could I use code in the AppDelegate to release ViewControllers and load new ones directly into the Window, by clicking a button which is set up in a ViewController?
Sorry if this is a bit of a stupid question, but I don't think I've quite grasped how the different layers relate, what code is available to which controller, etc.
Thanks
You should not put interface related code inside the appdelegate. That's what the ViewControllers are there for. If there's functionality which is common to all of your viewcontrollers, I'd suggest subclassing UIViewController (and then deriving all other instances from this subclass) or writing a category on it.
The appdelegate on the other hand is a good place to trigger basic events tied to the lifespan of the app, like setting up core data contexts or saving the application state when it is about to enter the background.
If what you are trying to do in viewDidLoad is have a proper "back" button in your UINavigationController, you don't need to do it that way. Simply define the title property of your controller to what you like. The controller's init method will do for that (no need in this case for viewDidLoad:
self.title = #"CHANGE";
or you can set it in the appDelegate:
controller.title = #"CHANGE";
As to your other question, best thing is start from scratch with a view based or window based project template.
Otherwise, you are quite right: you can release predefined controllers you don't need from the appDelegate and create your own. Additionally, you should take care of removing all those controllers' views from their parent:
[controller.view removeFromSuperview];
(for each controller you remove and for which there was a [self.window addSubview] in you delegate, possibly just the UiNavigationController).
Anyway, I don't know if I can suggest this path, because you would also need to fix all the dependencies related to the xib files, and would give you some headaches.

uiview in navigation

I am working with a navigation application. I have a homeViewController with two views(accept, service). When I click on a button in serviceView it should go to acceptView. I should have back button in navigation bar which takes me be back to serviceView from acceptView. When I am trying to use [self navigationController pushViewController, it only accepts viewcontroller but not view. Can any one please tell the alternative. Please help me.
Thanks in advance
You should have a different viewController for each view if you wish to use a navigationController properly.
Set up AcceptViewController and ServiceViewController separately. Then, from the AcceptViewController, you can create a ServiceViewController and push it onto the stack as follows:
-(void)showServiceView {
ServiceViewController *serviceViewController = [[ServiceViewController alloc] init];
[self.navigationController pushViewController:serviceViewController];
[serviceViewController release];
}
Assuming you've references to both acceptView and serviceView, you can just make this work by removing one as the subview and adding the other one as the subview of homeViewController's view. Something like,
[serviceView removeFromSuperview];
[self.view addSubview:acceptView];
for moving to acceptView. Switch them if you want to come back. However this mechanism will be abrupt. Use UIView's transitionFromView:toView:duration:options:completion: method to animate the transition. Something like,
[UIView transitionFromView:serviceView
toView:acceptView
duration:0.5f
options:UIViewAnimationOptionTransitionFlipFromLeft| UIViewAnimationOptionCurveEaseInOut
completion:NULL];
This will remove serviceView and add acceptView as the subview along with a transition to go by.

UINavigationViewContoller's rightBarButtonItem disappears once the application is pushed to background?

I have added rightBarButtonItem in a viewcontroller under viewWillAppear. its showing the button corretly.
- (void)viewWillAppear:(BOOL)animated {
UIBarButtonItem *addBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(add_Clicked:)];
self.navigationItem.rightBarButtonItem = addBarButtonItem;
}
but once i pushed application in background and bring it(applcation) to foreground the rightBarButtonItem got diappear?
I have debug that viewWillAppear get called once Application bring back to forground and the above line of code get executed also but then also the button does not comes up.
viewWillAppear is only called when you push a view on to the stack.
Place you code in the viewDidLoad.
When your app is send to the background, the viewDidUnload will be called, to reduce memory.
But sine your view is already presented it will not call the viewWillAppear.
Just tested this in a new Navigation-based project and I couldn't reproduce this.
One thing you can try is to call [super viewWillAppear:animated]; or check, if you setting the rightBarButtonItem to nil somewhere else.

What's the best way to implement a Back button on the Navigation Bar of an iPhone app

Fairly new to the coding community and took over an app from my developer. I want to implement a Back button on the navigation bar of one of the views in my app. If you guys could point me in the right direction, that'd be greatly appreciated.
Thanks very much!
in some scenarios, where you are not using an UINavigationController, you can create a UINavigationBar yourself.
self.navBar = [UINavigationBar new];
self.navBar.contentMode = UIViewContentModeTopLeft;
self.navBar.delegate = self;
self.navBar.frame = CGRectMake(0, 30, mainWindow.frame.size.width,40);
UINavigationItem *backButton = [[UINavigationItem alloc] initWithTitle:#"Back"];
[self.navBar setItems:[NSArray arrayWithObjects:backButton,[[UINavigationItem alloc] initWithTitle:#"Current View"], nil]];
[self.view addSubview: self.navBar];
Your controller should then implement the UINavigationBarDelegate protocol, so that you can respond to
-(BOOL) navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
where you can go back to your previous view.
There is mostly no use-case for this. You'll probably really want to work with UINavigationControllers and the push- and popViewController methods.
I hope this helped anyway.
kind regards.
The back button is added automatically when you use [UINavigationController -pushViewController]. Basically, instead of having a regular view controller, you need a navigation controller that is initialized with the default view, and when you want to show the other view, you need to push it on the navigation controller. By pushing the new view controller, it automatically adds the back button. The title of the back button is typically the title of the view controller, but you can change it with navigationBar.backItem.title.