I'm coding in Objective-C using XCode 4.6 on a MacBook Pro, and I want to know how to use the same button in my storyboard to get connected to two different screens, I mean, I want to put a condition estructure to decide to which screen the app is going when the user press the button, for example if(x==1) {go to view controller 1} else {go to view controller 2}
Connect segues from your view controller (not the button) in storyboard. Give the button an IBAction and throw your if statement in the implementation of the IBAction:
- (IBAction)buttonPressed:(id)sender
{
if (x == 1) {
[self performSegueWithIdentifier:#"Go to view controller 1" sender:self];
} else {
[self performSegueWithIdentifier:#"Go to view controller 2" sender:self];
}
}
Related
I have the following code in my custom method:
(IBAction)checkContinue:(UIButton *)sender {
if ([category.text isEqualToString:#""])
{
[self displayErrorMsg:#"Category Name cannot be empty"];
[category becomeFirstResponder];
}
//The else part doesn't work at all. When I click "Continue" the second view controller is shown and in that the error message in the "If" part is displayed.
else{
//This code will help to navigate from Menu types to Create Menu Screen
[self performSegueWithIdentifier:#"sw_contmenu" sender: self];
}
}
I have 2 segue for my MenuViewController: sw_contmenu and sw_newmenu, which will be executed from 2 different methods.
Problem: When I run the code and click Continue button, the above function is invoked with null values but the else part of the function doesn't work.
You have an extra action segue that is causing the second view controller to be pushed into view at the same you are handling the button press. These two operations are in conflict.
Deleting the action segue and adding a Show segue between the first view controller and second view controller should fix the problem.
I have a an app that has a sidebar menu. My side bar menu has 6 items on it. Items 0-4 will perform segue and move to destination view controller. But item 5 (feedback) when it is tapped should pop up an alert view and not move to another view controller. The solution I thought of is to use shouldPerformSegueWithIdentifier.
Here's what i've got so far:
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
BOOL isToDetail = true;
if([identifier isEqualToString:#"FEEDBACK"])
{
isToDetail = false;
}
else
{
isToDetail = true;
}
return isToDetail;
}
Now my concern here is, that everytime it goes thru this code the identifier is always null so it always goes to else instead of the if block. How can I get the identifier?
You should not use a segue to display an alert view because it is not a ViewController.
What you can do is create an IBAction in your code then link the tap event from the "item 5" to the IBAction.
What will happen is that when the button is tapped (usually the event is Touch Up Inside), it will call your IBAction and execute your code. You can find information on the new Alert Views of iOS below:
http://nshipster.com/uialertcontroller/
You have to specify the segue identifier in the Storyboard
Just open your Storyboard, select the segue, and on the Right panel add a unique identifier as showed here
I connected first view controller with the second one using StoryBoard Push Segue and Interface Builder.
The button is named GO on top/right.
I have three textfield that must be filled before going to second controller.
I display an alert when one of them is empty.
The problem is that my code after displaying correct alertView goes to SecondController instead of remaining on mainController.
if ([segue.identifier isEqualToString:#"DataDisplay"])
{
if (![self verifySelection]) {
return;
} else {
RowViewController *rowViewController = segue.destinationViewController;
// rowViewController.delegate = self;
}
}
1) You have a segue wired directly from your Go button to your Sensor Data view controller. You don't want this, because anytime someone touches Go, the segue is going to happen ... no stopping it. So, first step is to remove the segue you have going from Go to your second view controller.
2) Instead, wire the segue from the File's Owner icon below the view controller to the second view controller. Give it a name like DataDisplay.
3) In the IBAction for your Go button
if ([self verifySelection) {
[self performSegueWithIdentifier:#"DataDisplay" sender:self]
}
An easy fix would be to create the segue manually, rather than letting the interface builder manage it. So you would ctrl-drag from your main view controller to your second one, selecting push as the type of segue and assigning it an identifier through the identifier inspector, then you connect an IBAction to your Go button and in the method you perform the checks on the text fields before programmatically firing the segue with:
[self performSegueWithIdentifier:#"whateverIdentifierYouGaveYourSegue" sender:self];
Heads up: to create a manual segue from a viewcontroller to another one, you need to either zoom out in your storyboard or ctrl-drag from the yellow circle underneath the view!
Edit: Your IBAction connected to the button method should be something like the following:
- (IBAction)download:(id)sender {
if(text boxes are ok)
[self performSegueWithIdentifier:#"segueIdentifier" sender:self];
else
[self showWarning];
}
Make sure that you assigned the ID segueIdentifier to the segue you created in your storyboard.
Your problem is you are defining the "performSegueWithIdentifier" after displaying the alert.
I think the code you are doing is like this :
//AlertView Allocation
[alert show];
Perform Segue
If this is how you are doing, then you are doing it wrong.
You have to use the structure of If-Else Statements and put up the Perform Segue in the condition where all the textfields are filled.
Sorry for bad english.
I have MainViewController and TabBarController with two Views in storyboard.
when i push button (using modal segue) in my MainviewController - app switch to firstTabBarView or secondTabBarView depending on conditions. it works fine but after that i lost my TabBarControls at the bottom of any of my tabBarViews.
UPD: action after bar Button pressed
-(IBAction)barButtonPressed:(id)sender
{
if (condition !=0 ) {
[self performSegueWithIdentifier:#"segue1" sender:nil];
} else {
[self performSegueWithIdentifier:#"segue2" sender:nil];
}
}
It's working as advised. A modal segue is a "fullscreen segue" and covers your standard navigation stack.
If you simply want to change the selected viewcontroller, use
[yourTabBarControllerInstance setSelectedIndex:index];
Let me explain. I have multiple UIViewControllers. On my MainPageController, I have 3 UIViews. Let's enumerate it this way: the first UIView is called LoginView, the second is called HomeView and the other one is called RegView. Now in HomeView, there are multiple buttons that will lead to other UIViewControllers. For example, one button will lead to StoreController. Now if I am inside StoreController and I want to go back to MainPageController, I simply call:
[self dismissModalViewControllerAnimated:YES completion:nil]
This will send me back to the HomeView.
That is good. However, inside the StoreController, there are buttons which will supposedly direct me to LoginView or RegView, whichever button was tapped. The problem is when the method [self dismissModalViewControllerAnimated:YES completion:nil], it only take me back to HomeView, no matter which button I pressed.
So how will I display the right UIView once the dismissModalViewControllerAnimated is called?
EDIT:
This is how I show the UIViews:
-(void)viewDidLoad
{
//Initialize the views here...
}
-(void)showViewByTag:(NSInteger)tag
{
if (tag == 1)
{
[self.view addSubview:loginView];
}
else if (tag == 2)
{
[self.view addSubview:homeView];
}
else
{
[self.view addSubview:regView];
}
}
Now I call the method showViewByTag: somewhere in my code to display the views.
What you could try and do is following: before calling [self dismissModalViewControllerAnimated:YES completion:nil] (and thus go back to your home view), change the view currently displayed in your MainPageController:
[(MainPageController*)self.presentingViewController showViewByTag:desiredViewTag];
[self dismissModalViewControllerAnimated:YES...];
If you are worried at the cast and you foresee that self.presentingViewController might be not of MainPageController type on some occasions, then you can check explicitly for its type:
if ([self.presentingViewController isKindOf:[MainPageController class]])
[(MainPageController*)self.presentingViewController showViewByTag:desiredViewTag];
[self dismissModalViewControllerAnimated:YES...];
For this to compile, MainPageController.h must be imported in your modal controller class.
dismissModalViewController will always bring back the viewController which presented it ,and that can be only one,so the ideal way would be to tell the navigationController to initWith your desired viewController..
eg on regButton click in the presented modalview
RegViewController *regViewController = [[RegViewController alloc]initWithNibNam:#"RegViewController" bundle:nil];
[self.navigationController initWithRootViewController:regViewController];