dismissViewControllerAnimated not working - ios

I am using Storyboard with IOS6. I am trying to go back to the previous viewcontroller but it is not working.
I customized my backbutton here.
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.frame = CGRectMake(0, 0, 45,35);
[btnBack setTitle:#"Back" forState:UIControlStateNormal];
[btnBack.titleLabel setFont: [UIFont fontWithName:#"Courier" size:15]];
btnBack.layer.cornerRadius=5.0;
btnBack.layer.borderWidth=2.0;
[btnBack setBackgroundColor:[UIColor colorFbBlue]];
btnBack.layer.borderColor=[UIColor colorFbBlue].CGColor;
[btnBack setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btnBack addTarget:self action:#selector(Click_On_Btn_Back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
-(void)Click_On_Btn_Back{
[self dismissViewControllerAnimated:YES completion:nil];
}
This is how i push segue from previous view controller.
if([segue.identifier isEqualToString:#"segueFbShare"]){
FbShareViewController *fbVC=[segue destinationViewController];
fbVC.imageUrl=self.product.ImageUrl;
}

Going to previous UIViewController when using a UINavigationController:
[self.navigationController popViewControllerAnimated:YES];
Put that line of code in your Click_On_Btn_Back method

When using a Navigation Controller and push you need to remove the view by using:
- (void)Click_On_Btn_Back {
[self.navigationController popViewControllerAnimated:YES];
}
Also Click_On_Btn_Back is not general iOS naming convention. You should use something more like: clickOnBtnBack (CamelCase).

Related

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];

link button to view controller programmatically

I am starting with xcode and objective C, maybe my quiestions are basics even repetitive but really already I was looking, please no negative votes n_n
I want to link a button to view controller
Graphically with Xcode I can make it but I need to do it programmatically
button is created with the following code
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn1.frame = CGRectMake(40, btnp, 100, 30);
btn1.backgroundColor = [UIColor purpleColor];
[btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn1 setTitle:#"Ingresar" forState:UIControlStateNormal];
[self.scrolling addSubview:btn1];
this button is in principalviewcontroller
then I wanna that
when I click the button a new viewcontroller named "viewcontroller" is opened
how to I do?
Already I was looking and looking and looking but I have not found a solution
I have several days with this, help me please
[btn1 addTarget:self action:#selector(showSecondViewController) forControlEvents:UIControlEventTouchUpInside];
then create the method
- (void) showSecondViewController {
SecondViewController * secondVC = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondVC animated:YES];
}

How to make buttons that go to a different screen?

Hey I'm having a lot of trouble with this idea I have. How do I make buttons that go to a different screen but on the CODE?
Basically depending on a value that may or may not be different for every user (Let's say the value is in x already) it will make a list of buttons...
When you click the list of buttons, they each individually go to a different screen.
How do I do this? I know how to on storyboard.. but code wise?
Edit: I SHOULD note that for the buttons specifically I have this and it is working in the code (didn't use storyboard):
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(50, 200, 200, 50);
[btn setTitle:#"Button 1" forState:UIControlStateNormal];
[self.view addSubview:btn];
First add a target to your button:
//Add target to your button, to call a method that presents the next ViewController:
UIButton *yourButton = [[UIButton alloc]init];
[yourButton addTarget:self action:#selector(goToNextPageMethod) forControlEvents:UIControlEventTouchUpInside];
User one of the two, case you are using a UINavigation controller or not, place them in the method you are calling when the button is pressed (the target)
//Normal presentation
YourViewController *vc = [[YourViewController alloc]init];
[self presentViewController:vc animated:YES completion:nil];
//Navgiation bar
YourViewController *vc = [[YourViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];
Hope this helps
You can attach an action to your button by doing the following:
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(50, 200, 200, 50);
[btn setTitle:#"Button 1" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(someAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
and then in your someAction method, you push the view modally or via your navigation controller
- (void)someAction
{
//init your view controller here....
YourViewController *vc = [[YourViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];
}

Modal View Controllers - UIBarButtonItems Do Not Persist

I present modal view controllers, packaged in a UINavigationController.
When they first appear, the bar button items are alright.
However, if I click a button and a new view controller is pushed to the UINavigationController, and then I use the back button of that view controller, and it gets popped back to the original modal view controller, the buttons do not show up, (but they work when I click them -> just can't see them)
I can't seem to figure out why this occurs.
Here's some code.
Oh, and by the way, I customize the navigation controller's navigation bar's background. I have a feeling this may be causing this visual interference, but I don't know what to change to get it right.
In GGMainViewController:
GGSelectTeamsViewController *chooseTeam = [[GIFSelectTeamsViewController alloc] init];
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:chooseTeam];
[self setupModalNavigationBar:navigationController];
[self presentViewController:navigationController animated:YES completion:nil];
- (void)setupModalNavigationBar:(UINavigationController *)nav {
// unnecessary code removed for a quicker read
// basically navigation bar has a background gradient as well as a bottom border
[nav.navigationBar.layer addSublayer:bottomBorder];
[nav.navigationBar.layer addSublayer:gradient];
}
In GGSelectTeamsViewController
- (void)viewDidLoad
{
[super viewDidLoad];
title = [[UILabel alloc] init];
title.text = someText;
title.backgroundColor = [UIColor clearColor];
title.textColor = [UIColor whiteColor];
self.navigationItem.titleView = title;
[title sizeToFit];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
backButton.frame = someFrame;
[backButton addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:backButton] animated:NO];
UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
[selectButton setTitle:#"Select" forState:UIControlStateNormal];
[selectButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
selectButton.frame = someFrame;
[selectButton addTarget:self action:#selector(sendContent) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:selectButton] animated:NO];
}
Since the navigation items are set in the viewDidLoad method, why can't I see them (but they are still functional) when I return to it from another view controller?
That seems like an over-complicated way of doing this.
Why don't you create your own button and then use the setRightBarButtonItems:items: method from the UINavigationBar to set the buttons you want?
I'm guessing they disappear because you're adding them to the layer of the UINavigationBar.
Still, if you want to keep this method with the layers, you may want to add them in the viewWillAppear method, but you may need call the removeFromSuperlayer to make sure you don't add stuff more than once.
Hope this helps.
The problem was:
the gradients for the navigation bar (done by code)
The solution was:
use Sketch to create an image of the gradients and use the appearance proxy to set the image as a background image
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"NavigationBarBackground"] forBarMetrics:UIBarMetricsDefault];

Make UIButton act as navigationcontroller

How can I make a regular UIButton act as a navigationcontroller so that when it's pressed I can open up a new view?
Create yourButton in viewDidLoad method as following way...
UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[yourButton setTitle:#"YearButton" forState:UIControlStateNormal];
yourButton.frame = CGRectMake(240, 40, 75, 30);
[yourButton addTarget:self action:#selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yourButton];
This is your method code and createViewController which is object of CreateViewController class and it is declared in .h file.....
-(void) buttonSelected:(id)sender{
if (!createViewController) {
createViewController = [[CreateViewController alloc] initWithNibName:#"CreateViewController" bundle:nil];
}
[self.navigationController pushViewController:createViewController animated:YES];
}
I think it will be helpful to you.
Assuming you are using a UINavigation controller in your project.
then for the buttons action just call
[yourUIButton addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
and for the method that gets called just do the following:
-(void)buttonAction{
[self.navigationController pushViewController:YourViewController animated:YES];
}

Resources