link button to view controller programmatically - ios

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

Related

#Selector nothing happen

I Know this is probably very easy question, but i can't do that, i am using #selector to dismiss a view, writing this code, nothing happen:
- (void)viewDidLoad {
//here create my button
dismiss = [UIButton buttonWithType:UIButtonTypeRoundedRect];
dismiss.frame = CGRectMake(100, 10, 60, 50);
[dismiss setTitle:#"Close" forState:UIControlStateNormal];
[dismiss setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[dismiss addTarget:self action:#selector(closeView) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:dismiss];
}
//here the function
- (void)closeView {
[self dismissViewControllerAnimated:YES completion:nil];
}
But nothing happen, where am I doing mistake?
thanks
Try writing the addTarget line of code after [self.view addSubview:dismiss]; and check
That depends on what you're trying to do. dismissViewControllerAnimated:completion: will dismiss a controller that has been presented with presentViewController:animated:completion:, if there is no controller presented, it won't do anything.

UIButton won't open new view Controller

I am having trouble getting a new view controller to show on the screen when the button is pressed. I created a subclass of UIViewController named StartUpVC. I added it to the app delegate.m and created an instance of StartupVC *startUpVC = [StartUpVC new] and then set self.window.rootViewController = startUpVC;. So now in the StartUpVC class, I created a button so a user can log in with a UIImageViewin the background. Now when the button is pressed, it doesn't open up the view controller for the log in screen and does nothing. So my question is how do i get it to open that view controller? Or should it simply be a UIView that opens when the button is pressed?
This is in the StartUpVC.m
- (void)viewDidLoad
{
[super viewDidLoad];
...Code Irrelevant to the question here...
//Create Log In Button
UIButton *logInButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
logInButton.frame = CGRectMake(20, 480, 280, 40);
logInButton.layer.cornerRadius = 4.0f;
logInButton.layer.opacity = .6;
logInButton.backgroundColor = [UIColor lightGrayColor];
[logInButton setTitle:#"Log In" forState:UIControlStateNormal];
[logInButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[logInButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[logInButton addTarget:self action:#selector(sendToLogInScreen:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:logInButton];
}
-(void)sendToLogInScreen:(id)sender {
LogInVC *logIn = [LogInVC new];
[self presentViewController:logIn animated:YES completion:nil];
}
First thing you need to know is whether the method sendToLogInScreen is getting called at all. Place a breakpoint anywhere in the function to check.
Once you know whether the function is getting called at the proper time or not, this problem will either be about:
sendToLogInScreen is not getting called
Something is wrong with the button
it's nil
the gesture isn't setup properly
UserInteraction is disabled for StartupVC's view property
sendToLogInScreen is getting called
logIn is nil
presentViewController is failing for some reason
Hope that helps you figure out what might be causing the issue.

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.

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

Simultaneous push ViewController

I have a "strange" issue with xcode5 and the new sdk.
I have a Navigation controller.
The root view controller has 2 buttons, both buttons are linked to an action that push a viewcontroller into navigation-controller's stack.
If I run this project on XCode5 and if I push both buttons simultaneous the navigation controller going "crazy" with the message: "nested push animation can result in corrupted navigation bar".
But if I try the same code with xCode4 the application work also if I push simultaneous.
Have I forgot something??
Is this a already known behaviour?
There are a way to fix this issue?
Thanks a lot
This is the simple sample code
'
-(void)viewDidLoad
{
[super viewDidLoad];
UIButton *BTN1= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[BTN1 addTarget:self action:#selector(ACTION) forControlEvents:UIControlEventTouchUpInside];
[BTN1 setTitle:#"XXX" forState:UIControlStateNormal];
[BTN1 setFrame:CGRectMake(0, 100, 100, 100)];
UIButton *BTN2= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[BTN2 addTarget:self action:#selector(ACTION) forControlEvents:UIControlEventTouchUpInside];
[BTN2 setFrame:CGRectMake(0, 200, 100, 100)];
[BTN2 setTitle:#"XXX" forState:UIControlStateNormal];
[self.view addSubview:BTN1];
[self.view addSubview:BTN2];
}
-(void)ACTION
{
FirstViewController *fi = [[FirstViewController alloc] init];
[self.navigationController pushViewController:fi animated:YES];
}`
To prevent the navigation stack from being corrupted when pressing 2 buttons simultaneously, it is best to set exclusive touch to the buttons you are working with. This way you can prevent multiple triggers of the button action and prevent multiple ViewControllers from being pushed to the stack and corruption to happen.
[yourButton setExclusiveTouch:YES];
Use button.exclusiveTouch = YES; on each of your buttons.
You will need to hook them up to UIButtons and set the property in viewDidLoad for example.

Resources