iphone - how can I get a leak with this alertView? - iphone

this is on the main code:
NSString * title = NSLocalizedString(#"myTitle", #"");
NSString * cancelTitle = NSLocalizedString(#"dismiss", #"");
NSString * otherTitle = NSLocalizedString(#"noMoreTips", #"");
NSString * message = NSLocalizedString(#"myMessage", #"");
[self ShowAlertBox: title : message : cancelTitle : otherTitle];
This is the method
- (void) ShowAlertBox: (NSString *) title : (NSString *) myMessage : (NSString *) cancelButton : (NSString *) otherButton {
UIAlertView * alertView = [[UIAlertView alloc]
initWithTitle:title
message:myMessage
delegate:self cancelButtonTitle:cancelButton
otherButtonTitles:otherButton, nil ];
[alertView show];
[alertView release];
}
I have also tried to remove the [alertView release] from here and put it inside
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
without any success... it is still leaking!!
am I missing something?
thanks

There's no leak. It's likely a false positive.

Related

Unable to update uitableview rows

I am trying to update selected rows of UITableView from UIAlertView inside didSelectRowAtIndexPath. But I am facing a problem. Each time I try to update any row, it's only updates the first row irrespective of selection of row.
My code is:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedrow = nil;
if (searching) {
slCell=indexPath.row;
selectedrow = [copyListOfItems objectAtIndex:indexPath.row];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter the Number of Quantity" message:#""
delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert addTextFieldWithValue:#"" label:#"Number of Item"];
textfieldQty = [alert textFieldAtIndex:0];
textfieldQty.keyboardType = UIKeyboardTypeNumberPad;
textfieldQty.keyboardAppearance = UIKeyboardAppearanceAlert;
textfieldQty.autocorrectionType = UITextAutocorrectionTypeNo;
[alert show];
[alert release];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter the Number of Quantity" message:#""
delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert addTextFieldWithValue:#"" label:#"Number of Item"];
textfieldQty = [alert textFieldAtIndex:0];
textfieldQty.keyboardType = UIKeyboardTypeNumberPad;
textfieldQty.keyboardAppearance = UIKeyboardAppearanceAlert;
textfieldQty.autocorrectionType = UITextAutocorrectionTypeNo;
[alert show];
[alert release];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *b=nil;
if (buttonIndex == 0) {
if (searching) {
if([textfieldQty.text isEqualToString:b]) {
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Enter plz" message:#""
delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK"];
[alert2 show];
[alert2 release];
}
NSString *newqty = [[NSString alloc] initWithFormat:#"%#",textfieldQty.text];
DMSAppDelegate *d= (DMSAppDelegate *)[[UIApplication sharedApplication] delegate];
[d->tableDataQt replaceObjectAtIndex:slCell withObject:(id)newqty];
NSLog(#"tb%#",copyListOfQty);
}
else {
NSString *newqty = [[NSString alloc] initWithFormat:#"%#",textfieldQty.text];
DMSAppDelegate *d= (DMSAppDelegate *)[[UIApplication sharedApplication] delegate];
[d->tableDataQt replaceObjectAtIndex:slCell withObject:(id)newqty];
[self.tablevieww reloadData];
}
}
}
Please follow below steps to achieve what you want.
While creating UIAlertview in (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method, set alerview's tag as indexpath's row.
In delegate method -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex, you try to access alertView's tag and pass that tag to the method call [d->tableDataQt replaceObjectAtIndex:[alertView tag] withObject:(id)newqty];
In AppDelegate class, you create one more method called -(void)replaceObjectAtIndex:(NSInteger>inIndex withObject:(id)inObject
In above newly created method try to access UITableViewCell from UITableView object with method call UITableViewCell *myCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:inIndex inSection:0];
This way, you will get the object of UITableViewCell and alter the value of that and reload the table.
I hope this will help you. Please do not forget to put right mark if this resolves your problem. :)
Thanks.

UIAlertView to UITableViewController

When I press the 'OK' button at the UIAlertView I wanted it to go back to UITableViewController but when I click it doesn't go back.
QuizViewController.h:
#interface QuizViewController : UIViewController <UIAlertViewDelegate> {
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
#end
QuizViewController.m
-(IBAction)buttonWasClicked:(id)sender{
UIButton *resultebutton= (UIButton*)sender;
if (qCount < totalQuestions) {
id prevQuestion = [appDelegate.qs objectAtIndex:qCount-1];
NSString * correctAns = [prevQuestion labelAns];
if ([correctAns isEqualToString:resultebutton.titleLabel.text])
myScore += 5;
NSLog(#"The button title is %# ", correctAns);
NSLog(#"The button title is %# ", resultebutton.titleLabel.text);
NSString *finishingStatement = [[NSString alloc] initWithFormat:#"Your score so far is %i!", myScore];
theScore.text = finishingStatement;
id nextQuestion = [appDelegate.qs objectAtIndex:qCount];
quizLbl.text = [nextQuestion labelQn];
headerLbl.text = [nextQuestion labelHeader];
[qBtn setTitle:[nextQuestion labelBtn] forState:UIControlStateNormal];
[qBtnB setTitle:[nextQuestion labelBtnB] forState:UIControlStateNormal];
[qBtnC setTitle:[nextQuestion labelBtnC] forState:UIControlStateNormal];
[qBtnD setTitle:[nextQuestion labelBtnD] forState:UIControlStateNormal];
qCount++;
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Results" message:[[NSString alloc] initWithFormat:#"Your total score will be %i!", myScore]
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
QuizTableViewController *quizTable = [self.storyboard instantiateViewControllerWithIdentifier:#"quizTable"];
[self.navigationController presentModalViewController:quizTable animated:YES];
}
You should not declare a delegate method in your .h file like:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
instead give the delegate:self in alertView like this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Results" message:[[NSString alloc] initWithFormat:#"Your total score will be %i!", myScore]
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
and declare in your .h file like
#interface QuizViewController : UIViewController<UIAlertViewDelegate>
and then use [self.navigationController presentModalViewController:quizTable animated:YES];
in your delegate method at the click of button.index=0.
The reason why you don't get called back is that you set your UIAlertView delegate to nil. It needs to be set to self in order for that object to receive a callback when the alert is dismissed:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Results"
message:[[NSString alloc] initWithFormat:#"Your total score will be %i!", myScore]
delegate:self
cancelButtonTitle:#"OK" otherButtonTitles: nil];
Use this:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
if([alertView.title isEqualToString:#"the_title_of_your_alert"]){
//above line is to identify your alert, if you have several ones
if(buttonIndex == 0)
//do this
else if (buttonIndex == 1)
//do that
else if //bla bla bla, find the button, and if it is your "ok" button,
//go to your TableViewController
}
}
And alertView's delegates should not be declared that way, just implement the delegate methods.
Use this code to go back.
[self.navigationController popViewControllerAnimated:YES];

How do I set a password for a login and then segue to another ViewController when it is correct?

Here is my code:
- (IBAction)login:(id)sender {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Login"
message:#"Enter your username & password"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Login", nil];
[message setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[message show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Login"])
{
UITextField *username = [alertView textFieldAtIndex:0];
UITextField *password = [alertView textFieldAtIndex:1];
}
}
Create an Objective c class with two properties (userName and Password)..
Then modify your code
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Login"]) {
UITextField *username = [alertView textFieldAtIndex:0];
UITextField *password = [alertView textFieldAtIndex:1];
Validate your password and user name using **username.text and password.text**
if its valid then
//create an Object the for the class which holds UserName and Password and set the values.
newObj.userName = username.text;
newObj.password = password.text;
pass the object from here where you want'
release the newObj
}
}
When you've validated tbe username and password --
[self performSegueWithIdentifier:#"LoginSegue" sender:self];
Create "LoginSegue" in your storyboard from the controller that gets the login info to the controller you want to show after login.

iphone alertview with textfield

I have an UIAlertView with a UITextField in it. I want to type the mail id and submit in UIAlertView's ok button, but UITextField in the UIAlertView have no response, Please help me.
thankz
From iOS 5 the approach above is no longer necessary. Just set the alertViewStyle property to the appropriate style (UIAlertViewStyleSecureTextInput, UIAlertViewStylePlainTextInput, or UIAlertViewStyleLoginAndPasswordInput).
Example:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Email" message:#"Enter your email:" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView show];
and you can have the response back as
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField *emailTextField = [alertView textFieldAtIndex:0];
NSLog(#"%#",emailTextField.text);
}
UIAlertView with UITextField:
Usage:
NSString *title = NSLocalizedString(#"Your Title","");
NSString *placeholder = NSLocalizedString(#"Placeholder Text","");
NSString *message = NSLocalizedString(#"A message to the user.","");
NSString *cancel = NSLocalizedString(#"Cancel","");
NSString *okay = NSLocalizedString(#"Continue","");
prompt = [[AlertPrompt alloc] initWithTitle:title
placeholder:placeholder
message:message
delegate:self
cancelButtonTitle:cancel
okButtonTitle:okay];
[prompt show];
[prompt release];
.h
#import <Foundation/Foundation.h>
#interface AlertPrompt : UIAlertView <UITextFieldDelegate>
{
UITextField *textField;
NSString *enteredText;
}
#property(nonatomic,retain) UITextField *textField;
#property (nonatomic, retain, readonly) NSString *enteredText;
- (id)initWithTitle:(NSString *)title placeholder:(NSString *)placeholder message:(NSString *)message delegate:(id)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle;
#end
.m
#import ".h"
#implementation AlertPrompt
#synthesize textField;
#synthesize enteredText;
#pragma mark -
#pragma mark AlertPrompt Delegates
- (id)initWithTitle:(NSString *)title placeholder:(NSString *)placeholder message:(NSString *)message delegate:(id)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle {
if (self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okayButtonTitle, nil])
{
self.title = title;
self.message = [NSString stringWithFormat:#"%#\n\n\n",message];
self.delegate = delegate;
UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 75.0f, 260.0, 30.0)];
[theTextField setBackgroundColor:[UIColor clearColor]];
theTextField.borderStyle = UITextBorderStyleRoundedRect;
theTextField.textColor = [UIColor blackColor];
theTextField.font = [UIFont systemFontOfSize:18.0];
theTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
theTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
theTextField.returnKeyType = UIReturnKeyDone;
theTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
theTextField.placeholder = placeholder;
theTextField.delegate = self;
[self insertSubview:theTextField atIndex:0];
self.textField = theTextField;
[theTextField release];
CGAffineTransform translate = CGAffineTransformMakeTranslation(0, -10);
[self setTransform:translate];
}
return self;
}
- (void)show{
[textField becomeFirstResponder];
[super show];
}
- (NSString *)enteredText{
return textField.text;
}
- (void)dealloc{
[textField resignFirstResponder];
[textField release];
[super dealloc];
}
#end
Delegate:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if([alertView.title isEqualToString:#"Your Title"])
{
if(buttonIndex == 1)
{
/* get the user iputted text */
NSString *inputValue = [(AlertPrompt *)alertView enteredText];
NSLog(#"User Input: %#",inputValue);
{
}
}
UIAlertView *emailAlert=[[UIAlertView alloc]initWithTitle:#"Enter your Email-id" message:#"\n\n" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK",nil];
emailTextField=[[UITextField alloc]initWithFrame:CGRectMake(12,45,260,25)];
[emailTextField becomeFirstResponder];
[emailTextField setBackgroundColor:[UIColor whiteColor]];
emailTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
emailTextField.placeHolder=#"Email";
[emailAlert addSubview:emailTextField];
[emailAlert show];
[emailAlert release];
Use UIAlertView Delegates Methods when OK button is clicked.
Swift version:
var alert = UIAlertView(title: "PostActionTitle", message: "PostActionText", delegate: self, cancelButtonTitle:"PostActionDecline")
alert.alertViewStyle = UIAlertViewStyle.PlainTextInput
alert.addButtonWithTitle("PostActionAccept")
alert.show()

UIAlert View-for yes/no condition

my app needs alert msg and if yes button pressed then one more alert msg and then i have to called a method.This is my code:
-(IBAction)resetPressed:(id)sender
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag ==1)
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
alert.tag =2;
[alert show];
[alert release];
}
else if(alertView.tag ==2)
{
[self resetArray];
}
}
Thanks.
I'm not sure what your goal is but a few things look wrong to me anyways:
First of all you should create your strings this way:
NSString *title= #"Warning";
There's no need to use stringWithFormat in your case.
Then, it doesn't seem you properly set the first UIAlert's tag to 1, and the default value for tags is 0 so I guess the if statements in didDismissWithButtonIndex are never true.
Also, you should check which button was pressed using buttonIndex, otherwise you are going to show both alert and call [self resetArray] whichever button is pressed by the user.
Hope that helps.
In your code, you create the first alert, but never actually set the tag on it. You should do:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
alert.tag = 1; //Or 2, or something.
[alert show];
[alert release];
Then the code in your delegate method will run.
Please define two separate UIAlertView in .h file
#interface XYZViewController:UIViewController
{
UIAlertView *firstAlertView;
UIAlertView *secondAlertView;
}
Now in your .m file modify as below:
-(IBAction)resetPressed:(id)sender
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
if(firstAlertView == nil)
{
firstAlertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
}
[firstAlertView show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView == firstAlertView)
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
if(secondAlertView == nil)
{
secondAlertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
}
[secondAlertView show];
}
else if(alertView == secondAlertView)
{
[self resetArray];
}
}
and in dealloc method please release the allocated UIAlertviews.
Hope i am clear to you.
Thanks,
Jim.