ViewController called manually is over-ridden by "Show" Segue - ios

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.

Related

IBAction not getting fired

I have 2 view controllers in my app that user can navigate from one to another.
In my first view controller I dragged from an icon in the toolbar to the second view controller to setup a segues and selected “show” from the popup.
So far no issue, I can click on the icon in the toolbar and will take me to the second view controller without any problem.
However I have also created an action from that icon using drag and drop so now I have something like this
#IBAction func setting(sender: UIBarButtonItem) {
println("Test")
}
The problem I have is the setting action is not getting fired when I click on the toolbar icon, however it will navigate to the second view controller without a problem.
Reason I want to call the setting function is to perform something prior to moving the second view controller.
Do you see any problem with the way I have implemented this?
As you are using segue from your storyboard, then that segue is triggered before the button action.If you want to perform some action on your button click, then you have to manually call the segue like this
[self performSegueWithIdentifier:#"YourSegueName" sender:sender];
then instantiate your segue like this
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"YourSegueName"])
{
}
}

How can I get/pass the identifier to shouldPerformSegueWithIdentifier

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

Perform code when unwind segue with back button

I want to perform some code when the segue goes back one step. For example, when the back button gets selected, I want to perform some code.
I can't create a new unwind action from the back button because there is no back button in the storyboard. Is there a way to insert code, as soon as the back button is selected?
If you want to stick with the default back button, one way to do this is to subclass the navigation controller, and override popViewControllerAnimated: which is called when you tap the back button.
-(UIViewController *)popViewControllerAnimated:(BOOL)animated {
UIViewController *vcToBePopped =[super popViewControllerAnimated:animated];
id vc = self.viewControllers.lastObject; // this will be the controller you're going back to
if ([vc isKindOfClass:IntendedViewController class]) { // replace IntendedViewController with the actual class name you care about
[(IntendedViewController *)vc someMethod];
}
return vcToBePopped;
}

Conditional Segue Using Storyboard

I just need to know how to make some 'conditional' redirect / segue to some view. For example :
If user haven't login yet, then a view with login form appears. But if user logged in already, they will see their profile view.
How to make conditional segue like that using storyboard?
Thank you
For conditional segues, don't start a segue from a button as you normally do.
Instead of that use the following steps:
In your storyboad, start a segue directly from the source view controller to the destination view controller. For doing this, you can drag your source view controller's icon on the bar just below the view to the destination view controller on the main canvas area. Just remember you have to connect two view controllers directly and not with any control.
Enter a suitable segue identifier for this segue. For example say "conditionSegue"
Now, in your source view controller’s .m file perform your condition and call -performSegueWithIdentifier:sender: method on "self" like this:
-(void)loadDestinationVC{
if(condition == YES){
[self performSegueWithIdentifier:#"conditionSegue" sender:nil];
}
}
I hope I made it clear.
I believe a better approach if you want to use segues from buttons would be to use this:
First create a button and use a segue to connect it to another view, give this segue a name
then use this method in your .m file
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
if ([identifier isEqualToString:#"SegueName"]) {
//Check if should make the segue
//if not do something
[self doSomething];
//and return NO;
return NO;
}
return YES;
}
In Swift you could do :
as #Gaurav Singh suggested in his answer
Step 1: Insert a segue from ViewControllerA to ViewControllerB. The segue should start from ViewControllerA itself and not from any control ( like Button ) in it.
Step 2: Give segue an unique identifier in storyboard. Ex: seageFromAtoB
Step 3 : In your ViewControllerforA.swift write :
if(condition == true) {
self.performSegueWithIdentifier("seageFromAtoB", sender: self)
}
If you are performing some task in some other Thread then using performSegueWithIdentifier can throw an exception.
If you get this error then you should use :
if(condition==true){
NSOperationQueue.mainQueue().addOperationWithBlock {
self.performSegueWithIdentifier("seageFromAtoB", sender: self)
}
}
This will perform segue redirection in main Thread.
Hope this help for someone looking for Swift option
You make the segues directly from one controller to another (or others), and give them identifiers. In code you call choose between segues depending on your conditions, and call performSegueWithIdentifier:sender:.
Create "generic" segue by ctrl dragging from the view controller icon at the bottom to the destination. This segue won't be associated with any action. Then, in your code where ever the event is using your conditional code that you want to trigger the segue, call:
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender

IOS: Stay on the first view controller before complete all controls (StoryBoard)

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.

Resources