How to make buttons that go to a different screen? - ios

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

Related

iOS Button action doesn't work

I have a strange issue with iOS UIButton.
I have a UIViewController class:
#interface CustomViewController : UIViewController
Inside of this class I have a label called customLabel. In this label I have a button subview:
UIButton *addButton = [[UIButton alloc] init];
[customButton setImage:[UIImage imageNamed:#"image"] forState:UIControlStateNormal];
[customButton addTarget:self action:#selector(customButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[customLabel insertSubview:customButton atIndex:1];
And of course, action method:
- (void)customButtonClicked
{
NSLog(#"- (void)customButtonClicked");
}
I also have a subclass for this view controller:
#interface MyNewCustomViewController : CustomViewController
This view controller is loaded inside UIWindow:
[self.navigationController pushViewController: myNewCustomViewController animated:YES];
Everything seems to be fine, label with button are at the correct positions, except the fact, that button click doesn't call action method...
Maybe you know, where the problem could be?
As long as you don't have another view over top of your current button and user interaction is enabled on the view. This should work.
// First make sure userInteractionEnabled is enabled for your custom label which will contain your button as a subview
customLabel.userInteractionEnabled = YES;
// Then create and add your button as a subview
UIButton *addButton = [[UIButton alloc] init];
[customButton setImage:[UIImage imageNamed:#"image"] forState:UIControlStateNormal];
[customButton addTarget:self action:#selector(customButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[customLabel addSubview:addButton]; // Places view on top index

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 button that will go to a different screen in Objective C without storyboard?

I have this so far:
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];
Now I have someAction.. where do I put it and how do I do this? I'm sorry.. I hate asking questions.. but I looked over this everywhere and spent hours..
If you're using storyboards, and created a push segue, then the code looks like this
- (void)someAction
{
[self performSegueWithIdentifier:#"SomeSegueName" sender:nil];
}
If you're using storyboards, but don't have a segue, then the code looks like this
- (void)someAction
{
MyViewController *vc;
vc = [self.storyboard instantiateViewControllerWithIdentifier:#"SomeVC"];
[self.navigationController pushViewController:vc animated:YES];
}
If you aren't using storyboards at all, then I expect your head will eventually start hurting from banging it against the wall.

Slide down modal navigation

I have a UIViewController which contains a button. On the click of the button, it transitions as a modal (slide up transition) to a TabViewController which has two tab items.
On both the tab items, I have a Close button on which I want to transition back to the UIViewController mentioned above but in a slide-down transition.
How can I do that?
Let us say, you have button close target as dismissMe,
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 0, 100, 75);
button.titleLabel.text = #"Close";
[button setBackgroundColor:[UIColor greenColor]];
button.center = self.view.center;
[button addTarget:self action:#selector(dismissMe) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
then please add the following code to target method.
-(void)dismissMe {
[self dismissViewControllerAnimated:YES completion:nil];
}
Also you can use this instead
[self.parentViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
From the view controllers with the close button, call this when the button's UIControlEventTouchUpInside event is raised:
[self.parentViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];

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