UIAlertView button option to open new view - objective-c

I’m creating a tab based application. One of the tabs is test, and another of these tabs is results.
When the user has completed all the questions a UIAlertView message box pops up saying “Test Finished” and displays a “Ok” button to dismiss this message.
I’m wanting to change the title of this button to “Go to results” and can do this without any issues. But is there a way that enables me to direct the user to the results tab after clicking this message box button?
Any help would be appreciated. Thanks.

you have to set the selectedIndex of your UITabBarController
and you have to call this in the UIAlertViewDelegate method. So your method could look like this.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == alertView.cancelButtonIndex) {
// cancel... do nothing
}
else {
AppDelegate_Shared *appDelegate = [[UIApplication sharedApplication] delegate];
UITabBarController *tabBarController = appDelegate.tabBarController;
[tabBarController setSelectedIndex:0];
}
}
although, if you only have one button you don't need the if/else thingy.

Related

Dismiss uialertview programmatically in iOS7

I have implemented UIAlertview category in custom class. i am calling that method from my uitableviewcontroller to show an alert .But in iOS7 on iPhone 4 device, that alertview is coming multiple times while fast clicking on the uitableviewcells.
I am trying to dismiss the alertview if it is already opened by using the following code.
for (UIWindow* w in [UIApplication sharedApplication].windows)
for (NSObject* o in w.subviews)
if ([o isKindOfClass:[UIAlertView class]])
[(UIAlertView*)o dismissWithClickedButtonIndex:[(UIAlertView*)o cancelButtonIndex] animated:YES];
But this code is not working in iOS 7. I am not sure the way which i follow is correct or not.
Iterating the through [UIApplication sharedApplication].windows is a bad idea. Better concept is to hold the reference of an active UIAlertView and dismiss it when you want.
Note:
Make use of the UIAlertView property isVisible, which indicates whether the AlertView is displayed.
Example:
if (YES == alertViewInstance.isVisible)
{
//dismiss your alert view
}

Xcode storyboard how to detect touch item

I have a storyboard setup (the default) with the tabbed application template.
I want to add a BarItem and have that launch a URL when touched. I don't want it to go to a view in the app. How can I do this using the storyboard?
I am new the story board concept and not sure what to add to the story board to add a new bar item and then make it call a method.
First you need to put a function in the ViewController code something like...
- (IBAction)barButtonPressed:(id)sender
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"]];
}
Then go add the button to the view controller in the Storyboard and ctrl click drag from the button to the function.
This will make the button run that function when pressed.
If you mean the Navigation bar
//in viewDidLoad or wherever
UIBarButtonItem *openWebLink = [[UIBarButtonItem alloc]
initWithTitle: #"OpenWebLink"
style: UIBarButtonItemStyleBordered
target: nil action: #selector(openLink)];
[self.navigationItem setBackBarButtonItem: openWebLink];
-(void) openLink
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"]];
}
If you don't, look at Fogmeister answer
Code not tested!

iOS Beginner: UIAlertView Window with 3 Buttons > Check what button was pressed

I have a working code from a tutorial but don't understand it completely.
Situation:
After a button was pressed in my iPhone App
an AlertView appears with three buttons.
Now I like to check what button the user pressed.
CODE FROM THE TUTORIAL:
- (IBAction)infoButtonPressed:(id)sender {
UIAlertView *myAlert1 = [[UIAlertView alloc]initWithTitle:#"My Alert View 1"
message:#"Here we go"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Option1", #"Option2", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(#"Button: %i, was pressed.", buttonIndex);
}
Code works, I see the correct output in the console as a NSLog but how is it possible
that the method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(#"Button: %i, was pressed.", buttonIndex);
}
refers to the correct alert view. In this case: myAlert1.
What about with more than one alert view.
For example a second one calling myAlert2.
I know the following code is not correct but it would make more sense to me
if I'd write the method as follow:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(#"Button: %i, was pressed.", buttonIndex_FROM_myAlert1);
}
Hope you can help, drives me nuts.
Regards,
Marc
how is it possible that the method refers to the correct alert view?
For exactly that reason, the delegate method alertView:didDismissWithButtonIndex: actually tells you which alert view it refers to. Note that the method has two arguments. The second one tells you the button index and the first one points to the alert view this button index refers to.
If you have more than one alert view that share the same delegate, you will have to check against the first argument which alert view this is about. To be able to do that, you would have to store the alert views in an ivar/property or other data structure in order to remember them in the delegate method. (Or, since UIAlertView is a subclass of UIView, you could use the tag property to distinguish between multiple views).

Can you stop a modal view from dismissing?

I have a view that uses a modal view with a page curl to allow for a username to be entered. This username is then verified with a web-based service to see if it is valid.
Everything works great until you enter an invalid username and click outside the modal view. This still checks the username, which is reported invalid and a UIAlertView opens. However, it goes back to the parent view.
Is there any way to get the modal to not dismiss in this case?
I have tried to reload the view but either it isn't working or the UIAlertView is blocking it. The last idea I have is to couple displaying the modal view with the "OK" on the alert for an invalid username. Anyone have any ideas?
If you were not using a UINavigationController You could put something like this in the view controller that calls the modal view:
-(void)dismissModalViewControllerAnimated:(BOOL)animated{
if (_someFlagForBeingProperlyLoggedIn) [super dismissModalViewControllerAnimated:animated];
}
When you tap on the page curl the presenting/parent view controller is sent dismissModalViewControllerAnimated:.
Since you are using a navigation controller your options are limited. This is because UINavigationController is a subclass of UIViewController, and a self centered one at that. When you click the page curl it's dismissModalViewControllerAnimated: is being called.
You still have the option of subclassing UINavigationController and implementing the above method, but that will get messy in a hurry.
Having the UIAlertView "direct back" to the modal login view IS very easy. Have that main view conform to the UIAlertViewDelegate protocol. When you display the alert set that instance as the delegate, and in that class implement the method:
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
// Enclose in if (buttonIndex == #) for selective calling
UINavigationController* nav = (UINavigationController*)[[UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:#"Preferences"];
[nav setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self.navigationController presentModalViewController:nav animated:YES];
}
Then when the alert view is dismissed it will show the 'login' view.
You should redisplay your modal view with a little delay, something about 0.3-0.5. that's the amount of time needed to alert to be dismissed and that is exactly animation(the dismissing of the alert view) that prevent the modal view from showing up.
-(void)showModal{
SomeModalViewClass* modalView = [[SomaModalViewClass alloc]init];
[self setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentModalViewController:modalView animated:YES];
[modalView release];
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
//check the button index if needed and then
[self performSelector:#selector(showModal) withObject:nil afterDelay:0.3];
}

How to open a dialog box in iphone?

I want to open the dialog box when the user clicks on a browse button in the nib. This would search for a picture he wants from his pc and upload it. How do I do this programmatically in iphone.
I'm not sure if I understand exactly what you want. But if you just want to show view on top of another view you should user the - (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated method of the UIViewController.
When pressing the button you init a new UIViewController subclass that does what you wan't to do in this 'popup'. Then you present it to your current view controller by calling the presentModal... method. When you're finished you can call - (void)dismissModalViewControllerAnimated:(BOOL)animated on your 'popup' view controller to dismiss it.
I think i understand ur problem,if u want to have something like dilogBox in iPhone, then i have something for U. the are two links for .h and .m file.
FileChooserAlert.h and FilechooserAlert.m.After clicking on these two links u'll get required files.Now to implement define ur class like this.
FileChooserAlert* fileChooserAlert = [[FileChooserAlert alloc] initWithTitle:#"Select" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok",nil];
[fileChooserAlert show];
[fileChooserAlert release];
The two source file has something that u need to change before u can actually run it.Like there are three images named "File_icon.png","Folder_icon.png","Folder_up.png", which u need to include into ur project.
Now when user selects any file by selecting the tableView Cell and tapping OK button, U can get the file location by calling.
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == ALERT_TYPE_FILE_CHOOSER)
{
switch (buttonIndex)
{
case 0: //Cancel
break;
case 1:
{
myFileLocation = [(FileChooserAlert*)alertView getFileCompletePath]];
}break;
default:
break;
}
}
}
If U have any problem implementing this file email me.