Go back to unvisited page on IOS - ios

I am unfamiliar with IOS development, so probably a simple question: is it possible that the user presses the "back" button on the top, and go to a (so far) non-visited page?
Here is the picture:
On window "1" user presses a button, the next window is "2". But here, if user presses the "back" button, go to page "3" (which was never opened so far)?
Thanks

You can simply redefine the back button by creating a new one in your second view controller like this :
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 60.0f, 30.0f)];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
[backButton setTitleColor:self.view.tintColor forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(pushToNextController) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
}
- (void)pushToNextController {
UIViewController *thirdViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"thirdViewController"];
[self.navigationController pushViewController:thirdViewController animated:YES];
}

Related

UIBarButtonItem to go back is not showing up?

I have no idea why the back button is not being shown after i press a cell on a table. Now i read that on the parent controller (where you click on the cell to move on to the next view) that you should insert this code:
UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];
self.navigationController.navigationBar.tintColor = navBarItems;
And this is how the setup is done:
So my question is why is the back button item not showing up when i click on a cell?
Note:
The funny thing is that i am using a UIPageView where the back button is showing where i use the same code as i did in the parent view controller. So what am i missing? AND i know for sure that is was working on the previous IOS versions. This happened after upgrading maybe that will help you guys a bit?
Thank you
The back button is not being shown because, in the current UINavigationControllerthere is only the cell detail screen so it has nothing to go back to.
I think that you should put that UINavigationController in front of the UIViewController that holds your UITableView
Try This
UIButton *aBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[aBtn setImage:[UIImage imageNamed:#"YourImageName"] forState:UIControlStateNormal];
[aBtn setFrame:CGRectMake(0, 0,50, 64)];
[aBtn.titleLabel setFont:[UIFont systemFontOfSize:18.0]];
aBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
aBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
aBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[aBtn setTitleColor:[UIColor colorWithRed:1/255.0 green:65/255.0 blue:96/255.0 alpha:1.0] forState:UIControlStateNormal];
[aBtn addTarget:self action:#selector(popViewController) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *aBarBtn = [[UIBarButtonItem alloc]initWithCustomView:aBtn];
self.navigationItem.leftBarButtonItem = aBarBtn;
-(void)popViewController
{
[self.navigationController popViewControllerAnimated:YES];
}

Create a back bar button programmatically

I'm trying to create a back bar button programmatically, similar to the iOS 7 default back button. But when I add my back bar button to the navigation item. The Chevron plus is missing. How can I achieve this?
UIImage* image_back = [UIImage imageNamed:#"leftarrow.png"];
CGRect backframe = CGRectMake(15, 5, 15,21);
UIButton *backbutton = [[UIButton alloc] initWithFrame:backframe];
[backbutton setBackgroundImage:image_back forState:UIControlStateNormal];
[backbutton addTarget:self action:#selector(Btn_back:)
forControlEvents:UIControlEventTouchUpInside];
[backbutton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *backbarbutton =[[UIBarButtonItem alloc] initWithCustomView:backbutton];
self.navigationItem.leftBarButtonItem=backbarbutton;
[backbutton release];
-(IBAction)Btn_back:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}

how to show back button on navigation bar?

I was practicing an application with navigation bar. I used UIBarButton to go to the next screen from the main screen.
UIButton *moveLeft = [UIButton buttonWithType:UIButtonTypeCustom];
[moveLeft setTitle:#"Next Screen" forState:UIControlStateNormal];
moveLeft.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:12.0f];
[moveLeft.layer setCornerRadius:4.0f];
[moveLeft.layer setBorderWidth:1.0f];
[moveLeft.layer setBorderColor: [[UIColor redColor] CGColor]];
moveLeft.frame=CGRectMake(200, 100.0, 90.0, 30.0);
[moveLeft setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[moveLeft addTarget:self action:#selector(moveToNext) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* barbutton= [[UIBarButtonItem alloc] initWithCustomView:moveLeft];
self.navigationItem.rightBarButtonItem = barbutton;
But now when the second screen appears there is no navigation bar.
Here are the screen shots of the app
i tried UIBarButton for back also but still navigation bar is not coming on the second screen.
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:back];
My question how do i show the back button the second screen?? i am not making any custom button but the default is also not coming automatically.
Please guide me.
From your comments, you are presenting the next view controller, instead of this
[self presentViewController:secondScreen animated:YES completion:nil];
Do like this
[self.navigationController pushViewController:secondScreen animated:YES]
Suggestion
Instead of adding subView to the window, you should set the rootViewController of the window which is the recommended way. So change
[self.window addSubview:[navControll view]];
To
self.window.rootViewController = navControll;
No one answered the question correctly, I met the same question, and I have solve it by myself. My solution is delete the navigation controller before the child view controller , than control drag between the tableview controller to the child view controller, select the "show"
You have to use UINavigationController. It will be root view controller for your app. Put mainVC into it and then just pushViewController second vc.
You can access navigation controller via navigationController method of any vc(mainVC in your case).
Simply u can add custom back button to navigationBar
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, button_width, button_height)];
[backButton setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backAction) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

Changing Navigatiobar back button action in ios6/ ios 7 compatible applications

I have a navigation controller application, and I need to set the custom action for the navigation back bar button. Tried some workarounds and not yet to find a solution.
Tried
UIBarButtonItem *backBarItem = self.navigationItem.leftBarButtonItem;
backBarItem.target = self;
backBarItem.action = #selector(popToHomeViewController);
Result : No effect. Back button pops to just previous viewController in navigation stack
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithTitle:backBarItem.title style:backBarItem.style target:self action:#selector(popViewController)];
self.navigationItem.leftBarButtonItem = customBarItem;
Result : No effect. Back button pops to just previous viewController in navigation stack
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithTitle:#"back" style:backBarItem.style target:self action:#selector(popViewController)];
self.navigationItem.leftBarButtonItem = customBarItem;
Result:Now my selector got invoked perfectly and navigated to desired viewController. Here the issue is that the back button not as like as the native back button. It is not having the bold "<" character as I have not mentioned it. If added < character it needs to be changed for ios 6 compatibility.
Any better solution to ensure ios 6 and ios 7 compatible navigation back button with custom selector?
Try this simple example will help you..
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *buttonImage = [UIImage imageNamed:#"back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
[customBarItem release];
}
-(void)back {
[self.navigationController popViewControllerAnimated:YES];
}
Make sure you have an button image with the size of a navigation bar back button in your resource folder with name back.png
Feel free if any other assistance is required.
Happy Coding!!!!!
Try this
viewController.navigationItem.hidesBackButton = YES;
//set custom image to button if needed
UIImage *backButtonImage = [UIImage imageNamed:#"back"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:backButtonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[button addTarget:viewController action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height)];
[backButtonView addSubview:button];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
viewController.navigationItem.leftBarButtonItem = customBarItem;
and in back method you can customise
- (void)back {
//Your code
}

How to prevent animation of back button when switching views?

I cannot figure out how to disable the back-button animation that occurs in the navigation bar when switching from a tableview to a standard view (when a cell is selected). There is no obvious line of code that enabled animation to begin with. Here it is in gif-form:
The navigation buttons in the Facebook app do not animate, so it is possible.
It may be relevant to mention that I am using the ViewDeck library to create the Facebook-like tableView menu, i.e. swipe to the right to expose a table.
EDIT: solution is based on Hesham Abd-Elmegid's answer but modified to use a custom image...
UIImage *settingsImage = [UIImage imageNamed:#"back_button#2x.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(280.0, 10.0, 29.0, 29.0);
[backButton setBackgroundImage:settingsImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, 50, 30);
[backButton addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = customBarItem;
If you set a custom UIBarButtonItem as a left navigation item (instead of standard back button item), it will fade instead of slide in, just like in Facebook's app. Just create a simple method that will replace back button functionality by calling popViewControllerAnimated: on the navigation controller in which your detail view controller is contained.
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(goBack)] autorelease];
}
- (void)goBack
{
[self.navigationController popViewControllerAnimated:YES];
}
Note: UIBarButtonItem can also be set up with an image using initWithImage:style:target:action: method.
You could replace the back button with a custom UIButton. That way it won't animate on transition.
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:#"Back" forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, 50, 30);
[backButton addTarget:self action:#selector(onBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = customBarItem;
[customBarItem release];
You will have to find a PNG for the arrow shape of the back button though.

Resources