i'm writing a little IOS app using Xcode 7 IOS 9 (objective-c). Right now I'm trying to switch from the main storyboard to the second storyboard by pressing a button called "confirm". How can i write code in ViewController.m to conditionally go to the second storyboard? So if all input is correct, user presses confirm it will go to the new storyboard, else stay and display warning message. Thank you!
UIStoryboard *storyBoard;
if (yourcondition)
storyBoard = [UIStoryboard storyboardWithName:#"StoryBoard1" bundle:[NSBundle mainBundle]];
else
storyBoard = [UIStoryboard storyboardWithName:#"StoryBoard2" bundle:[NSBundle mainBundle]];
YourTargetViewController *targetCon = [stryBoard instantiateViewControllerWithIdentifier:#"TargetControllerID"];
[self.navigationController pushViewController:targetCon animated:YES];
Hope it helps..
Try this code. May be help you.
//button Actions
- (IBAction)loginbtnClicked:(id)sender
{
[self.tfEmail resignFirstResponder];
[self.tfPasswrd resignFirstResponder];
if ([self.tfEmail.text isEqualToString:#""] || [self.tfPasswrd.text isEqualToString:#""])
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#“Title” message:#"You are required to fill out all fields." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
return;
}
if ([self isEmailValid:self.tfEmail.text])
{
//put your required code here.
//Navigate to next view controller.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main” bundle:nil];
HomeVC *homeVC =[storyboard instantiateViewControllerWithIdentifier:#"HomeVCID"];
[self.navigationController pushViewController:chatVC animated:TRUE];
}
else
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#“Title” message:#"Please enter valid email address." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
Related
I am new to iPhone apps development. In my code, in first ViewController1 I have a textfield and a ok- button. So here
When I click ok button without entering a string, it shows an alert message.
When I enter the string and then press ok button, it should navigate to next ViewController2.
I am able to do the 1st part correctly when I have not connected the ok-button to the ViewController2 in Interface Builder. But when I connect, it navigates and then shows alert message.
Please tell me how to navigate to ViewController2 programmatically after I enter the string and press ok button.
This is how my code looks for Ok- button:
-(IBAction) okButton: (id) sender
{
if([textfield.text isEqualToString:#""])
{
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:#"Warning!!" message:#"Enter the file name" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
else if(textfield.text!= nil)
{
foldername1= [[NSString alloc] initWithString:[NSString stringWithString:textfield.text]];
NSLog(#"foldername is: %#", foldername1);
}
ViewController2 *view2= [[ViewController2 alloc] initWithNibName:#"Next View Name" bundle:nil];
[self.navigationController pushViewController:view2 animated:YES];
}
Segue-based navigation:
Remove the segue connecting OK button to View Controller 2 and add a manual segue between View Controller 1 and View Controller 2. Then implement the following code:
-(IBAction) okButton: (id) sender
{
if(textfield.text.length == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning!!"
message:#"Enter the file name"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
else
{
foldername1 = textfield.text;
NSLog(#"foldername is: %#", foldername1);
[self performSegueWithIdentifier:#"segueIdentifier" sender:self];
}
}
Code-based navigation:
-(IBAction) okButton: (id) sender
{
if(textfield.text.length == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning!!"
message:#"Enter the file name"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
else
{
foldername1 = textfield.text;
NSLog(#"foldername is: %#", foldername1);
ViewController2 *view2 = [[ViewController2 alloc] initWithNibName:#"Next View Name"
bundle:nil];
[self.navigationController pushViewController:view2 animated:YES];
}
}
This assumes you're using ARC and that foldername1 is a strong ivar / property.
If you assign a segue to a button, then you should not give it a click handler. Simply remove the segue from the button and the code should be Ok since you are using pushViewController:animated: to navigate instead of navigating with a segue.
Normally you would create a segue from view controller 1 to viwe controller 2, name it with an identifier, and activate that in code with the performSegueWithIdentifier:sender: method. Like this:
[self performSegueWithIdentifier:#"selectedIdentifierForTheSegue" sender:self];
For more info this is a pretty good tutorial for implementing navigation: http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/
just writedown
return;
below [alert show]; line then it just shows an alert and not navigate to the nextview.
try this
(IBAction) okButton: (id) sender
{
if(textfield.text.length==0)
{
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:#"Warning!!" message:#"Enter the file name" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
else if(textfield.text.length!=0)
{
foldername1= [[NSString alloc] initWithString:[NSString stringWithString:textfield.text]];
NSLog(#"foldername is: %#", foldername1);
}
ViewController2 *view2= [[ViewController2 alloc] initWithNibName:#"Next View Name" bundle:nil];
[self.navigationController pushViewController:view2 animated:YES];
}
To push view controller:
ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:#"ViewController2"
bundle:nil];
[self.navigationController pushViewController:vc2 animated:YES];
And also check this.
I am having problem with AlertView. I am trying to use the UIAlertView and after click ok it will return back to the previous screen but it do not seems to work any advice ?
if (xGPSCoordinate==0 && yGPSCoordinate == 0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Failed to load the to get your current location"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
return;
[self.navigationController popViewControllerAnimated:YES];
}
or
if (xGPSCoordinate==0 && yGPSCoordinate == 0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"iPoly"
message:#"Failed to load the to get your current location"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
[self.navigationController popViewControllerAnimated:YES];
return;
}
both doesn't work
For this purpose you've to use UIAlertView's delegate method.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
First use this in your #interface <UIAlertViewDelegate>
Then set the delegate, self.yourAlertView.delegate=self;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0)//first button which should be the OK button
{
[self.navigationController popViewControllerAnimated:YES];
}
}
use the delegate method of UIAlertView, see the answer given by iNoob. It does not make a sense if you write anything after the "return;" statement as the code below "return;" statement will never get executed.
refer apple developer link for more details on UIAlertView delegate http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html
or a simple tutorial on alert view
http://mobile.tutsplus.com/tutorials/iphone/uialertview/
You just need to implement UIAlerView Delegate Methods.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{}
write your code for Pop to previous Controller here.You can the clicked button index and on the basis of that you can use.Don't for Conform UIAlertViewDelegate to Interface.
Hi guys i have this i IBAction linked to a button:
- (IBAction)showCurl:(id)sender {
alert1 = [[UIAlertView alloc]initWithTitle:#"Loading" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert1 show];
}
and a clickedButtonIndex to auto run but somehow it doesn't load SecondViewController:
#pragma mark UIAlertView
- (void)alertView:(UIAlertView *)alert1 clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0){
SecondViewController *sampleView = [[SecondController alloc] init];
[sampleView setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentModalViewController:sampleView animated:YES];
}
else{
// Cancel prompt
}
}
Am i missing something here?
If you don’t give the alert view some button titles, there won’t be any buttons to tap on and that delegate method won’t get called.
- (IBAction)showCurl:(id)sender {
alert1 = [[UIAlertView alloc] initWithTitle:#"Loading" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert1 show];
}
Your code doesn't show a button
alert1 = [[UIAlertView alloc]initWithTitle:#"Loading" message:nil delegate:self cancelButtonTitle:**nil** otherButtonTitles:**nil**];
You pass nil as cancelButtonTitle AND nil as otherbuttonTitles, you should at least have one button title set.
I wanted to know whether the following code is okay or not. I am trying to dismiss the alertView automatically after 2 seconds (and without any buttons in the alertView) from the "timedAlert" method.
//this is in another method
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert show];
[alert release];
[self timedAlert];
}
-(void)timedAlert
{
[self performSelector:#selector(dismissAlert:) withObject:alert afterDelay:2];
}
-(void)dismissAlert:(UIAlertView *) alertView
{
[alertView dismissWithClickedButtonIndex:nil animated:YES];
}
If the cancelButton of the alertView is set to "nil", how will the "[alertView dismissWithClickedButtonIndex:0 animated:YES];" thing work??? I tried making the cancelButton "nil" and it worked, but cant figure out how....
P.S: I call the timedAlert method from another
Any help is appreciated! Thank you!
First let me say it would be better if you handle this with a custom view, but with that said the problem looks to be with
[alert release];
You are releasing the object before you are done with it (I am surprise it does not crash).
Do something like this
// other code
alert = [[UIAlertView alloc] initWithTitle:nil message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert show];
[self performSelector:#selector(dismissAlert:) withObject:alert afterDelay:3.0f];
}
-(void)dismissAlert:(UIAlertView *) alertView
{
[alertView dismissWithClickedButtonIndex:nil animated:YES];
[alertView release];
}
Your code should work, and you should have no problems. I have done this in one of my previous apps. The button is not displayed because the title is nil but I think the instance of the button still exists. Put a breakpoint before closing your alert and take a look at the alert variable, and check to see if there is a buttons array or something, that should tell you how that works.
After I choose a picture through the UIImagePickerController interface from the Photo Library, the Photo Library view stays displayed, even though I've called dismissModelViewControllerAnimated in imagePickerController:didFinishPickingImage:editingInfo.
Has anyone seen this? These are the three relevant methods I'm using:
- (IBAction)choosePictureFromLibrary:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsImageEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
else {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Error accessing Photo Library" message:#"This device does not support a Photo Library." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(NSDictionary*)editingInfo {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Picture picked!" message:#"You picked a picture!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker {
[picker dismissModalViewControllerAnimated:YES];
}
I would have thought that calling imagePickerController:didFinishPickingImage:editingInfo would completely dismiss the Photo Library view, but it doesn't seem to. Is there anything else I have to do to make it go away?
You need to access the viewController of the picker not the picker itself. Try this line instead.
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
You can just call
[self dismissModalViewControllerAnimated:YES];
to dismiss any modal view controller on top of the current view.
This makes sense since you present the view controller by calling:
[self presentModalViewController:picker animated:YES];
Just an update to the answers to this
[self dismissModalViewControllerAnimated:YES];
has been deprecated in iOS 6.0 so you now need to use.
[self dismissViewControllerAnimated:YES completion:nil];
Not a huge change but for anyone that looks at this question and they are using iOS 6.0 they will need an updated answer.
[self presentModalViewController:filePicker animated:YES];
has also been deprecated in favor of
[self presentViewController:filePicker animated:YES completion:nil];