I have a tabbed application with 2 tab views. I want to change the view from the second tab to the first when the user presses "OK" on an alert view.
The following code snippet works when I use it on an -IBAction button pressed, but it doesn't do anything when I use it inside the alert code:
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
Why does it not work when used as below, in my SeondViewController.m in my
- (void)viewDidLoad {
[super viewDidLoad];?
UIAlertView *alert =[[UIAlertView alloc]
initWithTitle:#"Camera and Target coincident"
message:msg
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
I'm just learning this, so anything you can offer would be helpful.
When I use the following code:
if ([ theProjection Trans_Initialise] == 1) {
NSString * msg = nil;
UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:#"Camera and Target coincident" message:msg delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:#"OK"]) {
self.tabBarController.selectedIndex = 0;
}
}
I get the error message on -(void) alertView "Invalid argument type void to unary expression" Am I doing something grammatical, or is it (quite possibly) something I just didn't understand?
First of all, you need a delegate call back from the alert view informing you about the button that was clicked.
Your alert view should be created like this :
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:#"Camera and Target coincident" message:msg delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
Then in your m file implement the function :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:#"OK"]) {
self.tabBarController.selectedIndex = 0;
}
}
Related
-(void) alertView: (UIAlertView *) alertVw clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *str = [NSString stringWithFormat:#"%d 。", buttonIndex];
UIAlertView *newAlertVw = [[UIAlertView alloc] initWithTitle:#"INFO" message:str delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[newAlertVw show];
}
UIAlertView *alertVw = [[UIAlertView alloc] initWithTitle:#"TITLE"
message:#"box message"
delegate:self
cancelButtonTitle:#"hide"
otherButtonTitles:#"T1", #"T2", nil];
[alertVw show];
if user touched the one of otherButtons, yes, we know which button that user touched.
then, how can I to get the title of button that user just touched?
thanks.
With the delegate on your UIAlertView set to self you can use this:
-(void)alertView:(UIAlertView*)alertView didDismissWithButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"Button Title -----> %#",[alertView buttonTitleAtIndex:buttonIndex]);
// or you can check if it equals to string
if([[alertView buttonTitleAtIndex:buttonIndex]isEqual:#"Enter"])
{
// your code goes here
}
}
The method is buttonTitleAtIndex: and the info is simply found on the UIAlertView doc page.
https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html#//apple_ref/occ/instm/UIAlertView/buttonTitleAtIndex:
I advise you to refer to the doc as often as you can this is how you will learn and remember.
Actually, if you want to use the delegate method you have to use -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex.
and not
-(void)alertView:(UIAlertView*)alertView didDismissWithButton At Index:(NSInteger)buttonIndex
see: https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html#//apple_ref/occ/intfm/UIAlertViewDelegate/alertView:didDismissWithButtonIndex:
I guess it was just a typo of Pancho.
Since the introduction of iOS 7, a lot of improvements and changes were made to Objective-C. I've created a UIActionSheet below and assigned the delegate to self so that I could program each button press to complete an action.
- (IBAction)sortButton
{
UIActionSheet *sortSheet = [[UIActionSheet alloc]
initWithTitle:#"Sort By"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Featured"
otherButtonTitles:#"Price", #"Brand", nil];
[sortSheet showInView:self.view];
}
This is the code I use to check against what the user did when they clicked on a specific button in the UIAlertView:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
{
UIAlertView *dueDateAlert = [[UIAlertView alloc]
initWithTitle:#"Featured"
message:#"This button formats the list for items based on Feature."
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[dueDateAlert show];
}
else if(buttonIndex == 1)
{
UIAlertView *creationDateAlert = [[UIAlertView alloc]
initWithTitle:#"Price"
message:#"This button formats the list for items based on Price."
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[creationDateAlert show];
}
else if(buttonIndex == 2)
{
UIAlertView *subjectAlert = [[UIAlertView alloc]
initWithTitle:#"Brand"
message:#"This button formats the list for items based on Brand."
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[subjectAlert show];
}
}
When I run this code, I get the following Warning: Sending SecondViewController *const_strong to parameter of incomplete type 'id<UIActionSheetDelegate> on the self text in the creation of the UIActionSheet. I'm assuming it's saying that I'm doing something "inefficiently", and I was wondering what needs to change in order for this warning not to appear.
It seems that your class does not confirm to UIActionSheetDelegate protocol.
Check that you have something like
#interface YourClass () <UIActionSheetDelegate>
First time working with UIAlertViews and I can't figure out why this doesn't work. When I click on either button, the alert closes, but there is no NSLog output. What am I doing wrong? I can't figure it out. Thanks!
.h
#interface LoginViewController : UIViewController <UIAlertViewDelegate>
#end
.m
- (IBAction)resetPassword:(UIButton *)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Password Reset"
message:#"A temporary password will be sent to you at the address we have on file."
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Reset Password", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) NSLog(#"button 0");
if (buttonIndex == 1) NSLog(#"button 1");
}
It appears that you never set the delegate on the UIAlertView. It should be:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Password Reset"
message:#"A temporary password will be sent to you at the address we have on file."
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Reset Password", nil];
This way the UIAlertView is able to call your method, - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex, when the user taps a button.
You're setting the delegate to nil. You need to set it to your controller instance, like this:
- (IBAction)resetPassword:(UIButton *)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Password Reset"
message:#"A temporary password will be sent to you at the address we have on file."
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Reset Password", nil];
[alert show];
}
Implementing the -alertView:clickedButtonAtIndex: delegate method does little good if you never give the UIAlertView an object to serve as the delegate.
Hello to everyone i would like to place my thinking about an alertview. I am thinking to create an Uialertview which will prompt the user to input two integers. Then i would like to retrieve these two integers and put the one on a timer and the second in an sql statement. So if anyone can help on how to implement that i would apreciate it. Thank you all.
Here is my code until now
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"info" message:#"Set time For The Game." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:#"cancel", nil];
alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
NSLog(#"Entered: %#", [[alertView textFieldAtIndex:0] text]);
[alertView show];
You need to use delegate methods of UIAlterView.
First of all make "self a delegate" instead of nil.
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"info" message:#"Set time For The Game." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"cancel", nil];
Than add this function to your class:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField *field = [alertView textFieldAtIndex:0];
NSLog(#"%#", field.text);
}
To be 100% correct add also in your header class information that you implement this protocol .
// Try this
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"info" message:#"Set time For The Game." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"cancel", nil];
alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
[alertView textFieldAtIndex:0].delegate = self;
[alertView show];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"Entered: %#", [[alertView textFieldAtIndex:0]text]);
}
I want to delete data in UITableView using plist and i want to put alertview when delete button is pressed. I want to mention that i have used edit button to delete the data from tableview. code of edit button is as follows: for your information.
self.navigationItem.leftBarButtonItem=self.editButtonItem;
Plz help me solve this problem.
thanx in advance.
In your function write :
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: #"" message: #"" delegate: self cancelButtonTitle: #"OK" otherButtonTitles: nil];
alertView.tag = 1;
[alertView show];
[alertView release];
On alert "OK" button pressed
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0)
{
if (alertView.tag == 1)
{
//Do your code
}
}
}
Hope it helps...
on you button action write:-
-(IBAction)btnPressed
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil,nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alert didDismissWithButtonIndex:(NSInteger)buttonIndex
{
}