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
{
}
Related
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;
}
}
-(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.
I have this UIAlertview with tags that I am trying to use to call a function. The fogotpassword alert view shows up then the reset alertview shows up, but then when I am trying to call the NSLog(#"Password"); function by pressing the first button in reset alertview it doesn't get called. Instead, the reset alertview button pops up again. I will appreciate any help I get.
-(void)viewDidLoad{
forgotPassword = [[UIAlertView alloc] initWithTitle:#"Login Error"
message:#"Your login credentials do not match"
delegate:self
cancelButtonTitle:#"Try Again"
otherButtonTitles: #"Forgot Password",nil];
[forgotPassword show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
forgotPassword.tag = 1;
resetPassword.tag = 2;
if (buttonIndex == 1 && forgotPassword.tag ==1)
{
resetPassword = [[UIAlertView alloc] initWithTitle: #"Forgot Password"
message:#"Email" delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Reset Password", nil];
[resetPassword setAlertViewStyle:UIAlertViewStylePlainTextInput];
[resetPassword show];
NSLog(#"RESET");
}
if (buttonIndex == 1 && resetPassword.tag == 2) {
NSLog(#"Password");
}
}
Your logic is all messed up. You set both tags so both will always be true. Since you appear to have ivars for the two different alert views, get rid of the tags and do this:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView == forgotPassword) {
if (buttonIndex == alertView.firstOtherButtonIndex) {
// show other alert
}
else if (alertView == resetPassword) {
if (buttonIndex == alertView.firstOtherButtonIndex) {
// reset
}
}
}
No need to use tag
Try in this way
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertView==forgotPassword)
NSLog(#"forgotPassword alert");
}
Your code is confused. You create an alert view "forgotPassword" and show it, without setting it's tag. Then, in your alertView:clickedButtonAtIndex: method, you explicitly set the tag on both alert views, and THEN interrogate those alert views to see what their tags are. Those tags will never change.
Instead, you should set the tag on each alert view before showing it, and then check the tag of the UIAlertView that is passed in to the alertView:clickedButtonAtIndex: method. Something like this:
-(void)viewDidLoad{
forgotPassword = [[UIAlertView alloc] initWithTitle:#"Login Error"
message:#"Your login credentials do not match"
delegate:self
cancelButtonTitle:#"Try Again"
otherButtonTitles: #"Forgot Password",nil];
forgotPassword.tag = 1;
[forgotPassword show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1 && alertView.tag ==1)
{
resetPassword = [[UIAlertView alloc] initWithTitle: #"Forgot Password"
message:#"Email" delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Reset Password", nil];
[resetPassword setAlertViewStyle:UIAlertViewStylePlainTextInput];
resetPassword.tag = 2;
[resetPassword show];
NSLog(#"RESET");
}
if (buttonIndex == 1 && alertView.tag == 2) {
NSLog(#"Password");
}
}
You should be using alertView.tag == 1 and alertView.tag == 2 in your methods. As you have it right now, both if statements will always be true as long as buttonIndex == 1 since you explicitly set forgotPassword.tag = 1 and resetPassword.tag = 2, then check each item.
Also, you should set the tags within your viewDidLoad unless you have some reason to keep resetting their values every time an alertView button is pressed.
Or, an easier way would be what Anand K said.
-Stephen
Your code is redundant and in any case hasn't a sense..
You don't need to use UIAlertView as property in this case..
Use this:
-(void)viewDidLoad{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Login Error"
message:#"Your login credentials do not match"
delegate:self
cancelButtonTitle:#"Try Again"
otherButtonTitles: #"Forgot Password",nil];
alertView.tag = 1;
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
if(alertView.tag == 1)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: #"Forgot Password"
message:#"Email" delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Reset Password", nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
alertView.tag = 2;
[alertView show];
NSLog(#"RESET");
}
} else if (alertView.tag == 2) {
NSLog(#"Password");
}
}
This code is clean use it ;)
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 am trying to perform the smile task of having two alerts in a single View Controller. The code below works fine, but how would I make another instance of it elsewhere in the View Controller. I am concerned that if I duplicated the code, my buttonIndex would not know which alert it is responding to. Any ideas? Thanks!
-(void)alertChoice
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Message" delegate:self
cancelButtonTitle:#"Cancel" otherButtonTitles:#"Confirm", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
//do something
}
}
You can use the tag property on UIAlertView to decipher which alert is which:
-(void)alertChoice
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Message" delegate:self
cancelButtonTitle:#"Cancel" otherButtonTitles:#"Confirm", nil];
alert.tag = 0;
[alert show];
}
-(void)alertChoice1
{
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Message" delegate:self
cancelButtonTitle:#"Cancel" otherButtonTitles:#"Confirm", nil];
alert1.tag = 1;
[alert1 show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 0)
{
}
}
Simply set a tag to each Alert view and check which one sent the messeg.
alertView.tag=0;
And then
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag==0){
if(buttonIndex == 0)//OK button pressed
{
//do something
}
else if(buttonIndex == 1)//Annul button pressed.
{
//do something
}
}else{
if(buttonIndex == 0)//OK button pressed
{
//do something
}
else if(buttonIndex == 1)//Annul button pressed.
{
//do something
}
}
set tag to alert view.
alert.tag = 1;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1 && alertView.tag == 1)
{
//do something
}
}
Step 1: Add UIAlertViewDelegate in your view controller.h file
step 2: Add the following methods in your view controller.m file
-(void)AlertMethodOne
{
UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:#"AlertMethodOne" message:#"AlertMethodOne successfully Called" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil];
alert.tag=101;
[alertview show];
}
-(void)AletrMethodTwo
{
UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:#"AletrMethodTwo" message:#"AlertMethodTwo successfully Called" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil];
alert.tag=102;
[alertview show];
}
call the above two methods in your viewController as like below:
[self AlertMethodOne];
[self AlertMethodTwo];
Now AlertView Button Clicked Method
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag==101)
{
if (buttonIndex == 0)
{
}
}
if(alertView.tag==102)
{
if (buttonIndex == 1)
{
}
}
}