I have a UIAlertView with an image and a large button.
The problem is that the default UIAlertView is a very small size. It's image cannot be seen properly.
So, I want to increase the height and width of the UIAlertView. In other words, I want to create a custom UIAlertView. What would be the way to do that?
Thanks.
I am sending you my code, I am showing a TableView in AlertView, perhaps it will help you.
in header file paste this code.
#import <UIKit/UIKit.h>
#interface aaViewController : UIViewController <UIAlertViewDelegate, UITableViewDelegate, UITableViewDataSource>{
UITextField *textfield;
UITableView *tableView1;
NSMutableArray *_data;
}
#property (nonatomic, retain) NSMutableArray *_data;
- (void) showAlertView;
- (void) doAlertViewWithTextView;
- (void) doAlertViewWithTableView;
#end
and in your imlpementation(.m) file paste this code.
#import "aaViewController.h"
#implementation aaViewController
#synthesize _data;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self._data = [NSMutableArray arrayWithObjects:#"Najeeb", #"Furqan", #"Khalid", nil];
[self doAlertViewWithTableView];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
- (void) doAlertViewWithTextView {
UIAlertView *alert = [[UIAlertView alloc] initWithFrame:CGRectMake(0, 0, 300, 550)];
alert.title = #"Textview";
alert.message = #"I am doing cool stuff\n\n\n";
alert.delegate = self;
[alert addButtonWithTitle:#"Cancel"];
[alert addButtonWithTitle:#"OK"];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(12, 90, 260, 200)];
textView.text = #"This is what i am trying to add in alertView.\nHappy New Year Farmers! The new Winter Fantasy Limited Edition Items have arrived! Enchant your orchard with a Icy Peach Tree, and be the first farmer among your friends to have the Frosty Fairy Horse. Don't forget that the Mystery Game has been refreshed with a new Winter Fantasy Animal theme! ";
textView.keyboardAppearance = UIKeyboardAppearanceAlert;
[alert addSubview:textView];
[textView release];
[alert show];
[alert release];
}
- (void) doAlertViewWithTableView {
NSLog(#"Now play with TableView \n%#", self._data);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Table View" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
tableView1 = [[[UITableView alloc] initWithFrame:CGRectMake(10, 40, 264, 200) style:UITableViewStyleGrouped] autorelease];
tableView1.dataSource = self;
tableView1.delegate = self;
[alert addSubview:tableView1];
[alert show];
[alert release];
}
//#define kTag_EnterNameAlert 1
//#define kTag_NameEmtryField 100
- (void) showAlertView {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Congratulations!"
message:#"You earned a top score! Enter your name:\n\n"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"OK", nil];
//alert.tag = kTag_EnterNameAlert;
CGRect entryFieldRect = CGRectZero;
if( UIDeviceOrientationIsPortrait( [UIApplication sharedApplication].statusBarOrientation ) ) {
entryFieldRect = CGRectMake(12, 90, 260, 25);
}
else {
entryFieldRect = CGRectMake(12, 72, 260, 25);
}
textfield = [[UITextField alloc] initWithFrame:entryFieldRect];
//textfield.tag = kTag_NameEmtryField;
textfield.backgroundColor = [UIColor whiteColor];
textfield.keyboardType = UIKeyboardTypeAlphabet;
textfield.keyboardAppearance = UIKeyboardAppearanceAlert;
textfield.autocorrectionType = UITextAutocorrectionTypeNo;
textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
[alert addSubview:textfield];
[textfield becomeFirstResponder];
[textfield release];
[alert show];
[alert release];
}
# pragma -
# pragma alertView frame Methods
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog(#"name:%# buttonID:%d",textfield.text,buttonIndex);
if (buttonIndex == 1) {
[self doAlertViewWithWebView];
}
else {
NSLog(#"%#",alertView.message);
}
}
// to set the alertView frame size.
- (void)willPresentAlertView:(UIAlertView *)alertView {
[alertView setFrame:CGRectMake(10, 100, 300, 320)];
for ( UIView *views in [alertView subviews]) {
NSLog(#"%#",views);
if (views.tag == 1 || views.tag == 2) {
[views setFrame:CGRectMake(views.frame.origin.x, views.frame.origin.y+200, views.frame.size.width, views.frame.size.height)];
}
}
}
# pragma -
#pragma mark TableView Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 1) {
return [_data count];
}
else
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* const SwitchCellID = #"SwitchCell";
UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:SwitchCellID];
if( aCell == nil ) {
aCell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SwitchCellID] autorelease];
aCell.textLabel.text = [NSString stringWithFormat:#"Option %d", [indexPath row] + 1];
aCell.selectionStyle = UITableViewCellSelectionStyleNone;
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
switchView.tag = [indexPath row]+1;
aCell.accessoryView = switchView;
[switchView setOn:YES animated:NO];
[switchView addTarget:self action:#selector(soundSwitched:) forControlEvents:UIControlEventValueChanged];
[switchView release];
}
return aCell;
}
- (void) soundSwitched:(UISwitch*) switchView {
if (!switchView.on) {
NSLog(#"chal bhag %d", switchView.tag);
}
}
#end
Note: In your AppDelegate add as subView to this.
Related
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
warning: 'UIAlertView' may not respond to '-addTextFieldWithValue:label:'
I am using below code for put textfield in AlertView, it gives a warning like "UIAlertView may not respond - addTextFieldWithValue: label:" & "UIAlertView may not respond - textFieldAtIndex"...
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Email" message:#""
delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Submit", nil];
[alert addTextFieldWithValue:#"" label:#"Email"];
// Username
textfieldName = [alert textFieldAtIndex:0];
textfieldName.keyboardType = UIKeyboardTypeEmailAddress;
textfieldName.keyboardAppearance = UIKeyboardAppearanceAlert;
textfieldName.autocorrectionType = UITextAutocorrectionTypeNo;
[alert show];
please help!
there is no method "addTextFieldWithValue" for uialertview
if you need to modify your alert view have a look at my blog here - http://www.makebetterthings.com/blogs/iphone/add-uitextfield-in-uialertview/
These is no such methods like addTextFieldWithValue in UIAlertView.
[alert addTextFieldWithValue:#"" label:#"Email"]; is a private api. My app got rejected because of using this.
Use this
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:#"Twitter"
message:#"\n\n\n\n"
delegate:self
cancelButtonTitle:#""
otherButtonTitles:#"", nil];
myAlert.frame = CGRectMake( 0, 0, 300, 260);
textfieldName = [[UITextField alloc] initWithFrame:CGRectMake(13, 95, 260, 25.0)];
[textfieldName setBackgroundColor:[UIColor whiteColor]];
textfieldName.placeholder = #"xyz";
textfieldName.keyboardType = UIKeyboardTypeAlphabet;
textfieldName.keyboardAppearance = UIKeyboardAppearanceAlert;
textfieldName.autocorrectionType = UITextAutocorrectionTypeNo;
textfieldName.delegate = self;
textfieldName.tag = 1;
[myAlert addSubview:textfieldName];
**I use in my project < and use and implement
use in .h file**
#protocol InputAlertViewDelegate
-(void)handleEnteredValue:(NSString*)_value;
-(void)handleCancellation;
#end
#interface InputAlertView : UIAlertView <UIAlertViewDelegate> {
UITextField *textField;
id<InputAlertViewDelegate> caller;
} #property(nonatomic, retain) UITextField *textField;
#property(nonatomic, retain) id<InputAlertViewDelegate> caller;
-(NSString*) theText;
-(void) prepare;
-(void) makePassword;
-(void) makeTelephone;
+(InputAlertView*) inputAlertViewWithTitle:(NSString*)_title initialFieldText:(NSString*)_text caller:(id<InputAlertViewDelegate>)_caller;
**use in .m file**
+(InputAlertView*) inputAlertViewWithTitle:(NSString*)_title initialFieldText:(NSString*)_text
caller:(id<InputAlertViewDelegate>)_caller
{
InputAlertView *_alert =
[[[self alloc] initWithTitle:_title message:#"\n" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil] autorelease];
_alert.delegate = _alert;
_alert.caller = _caller;
[_alert prepare];
_alert.textField.text = _text;
return _alert;
}
-(void)prepare
{
self.textField = [[[UITextField alloc] initWithFrame:CGRectMake(12.0, 45, 260.0, 30.0)] autorelease];
[textField setBackgroundColor:[UIColor clearColor]];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
[self addSubview:textField];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 130.0);
[self setTransform:myTransform];
-(void) show{
[textField becomeFirstResponder];
[super show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 1){ [self.caller handleCancellation];
} else{
[self.caller handleEnteredValue:[self theText]];
}
}
i'm working on an app, but i have some problems with auto dismiss alert view and switch to another views.
Here is my code:
This is file "Progress.h":
#import <Foundation/Foundation.h>
#import "Progress_ToolViewController.h"
#interface Progress : UIAlertView {
UIProgressView *myPro;
//UILabel *myPercent;
UILabel *myCount;
NSTimer *timer;
int count;
BOOL userDismissed;
}
#property (nonatomic, retain)UIProgressView *myPro;
//#property (nonatomic, retain)UILabel *myPercent;
#property (nonatomic, retain)UILabel *myCount;
-(void)start;
-(void)moreprogress;
-(void)makecount;
-(id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okButtonTitle;
#end
Here is file "Progress.m":
#import "Progress.h"
#import "Progress_ToolViewController.h"
#implementation Progress
#synthesize myPro;
#synthesize myCount;
//#synthesize myPercent;
- (id)initWithTitle:(NSString *)title 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])
{
UIProgressView *thePro = [[UIProgressView alloc] initWithFrame:CGRectMake(12.0, 60.0, 220.0, 20.0)];
UILabel *theLabel = [[UILabel alloc] initWithFrame:CGRectMake(240.0, 55.0, 60.0, 20.0)];
[thePro setBackgroundColor:[UIColor clearColor]];
[theLabel setBackgroundColor:[UIColor clearColor]];
[theLabel setTextColor:[UIColor whiteColor]];
theLabel.text = 0;
[self addSubview:thePro];
[self addSubview:theLabel];
self.myPro = thePro;
self.myCount = theLabel;
myCount.text = #"0";
[self start];
[thePro release];
[theLabel release];
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 65.0);
[self setTransform:translate];
}
return self;
}
-(void)start{
myPro.progress = 0.0;
//myPro.text = #"%";
timer = [NSTimer scheduledTimerWithTimeInterval:0.10 target:self selector:#selector(moreprogress) userInfo:nil repeats:YES];
timer = [NSTimer scheduledTimerWithTimeInterval:0.20 target:self selector:#selector(makecount) userInfo:nil repeats:YES];
}
-(void)moreprogress{
myPro.progress = myPro.progress + 0.02;
if(myPro.progress == 1.0){
[timer invalidate];
[self dismissAlert];
}
}
-(void)makecount{
count = count + 4;
myCount.text = [[NSString alloc] initWithFormat:#"%i", count];
if(count == 100){
[timer invalidate];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([alertView tag] == 1) {
if (buttonIndex == 1) {
UIAlertView *subAlert = [[UIAlertView alloc] initWithTitle:#"Sub-Alert" message:#"You've chosen \"Sure\"." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[subAlert show];
[subAlert release];
}
else if (buttonIndex == 2) {
UIAlertView *subAlert = [[UIAlertView alloc] initWithTitle:#"Sub-Alert" message:#"You've chosen \"Not sure\"." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[subAlert show];
[subAlert release];
}
}
else if ([alertView tag] == 2) {
userDismissed = YES;
}
}
-(void)dismissAlert{
if(userDismissed) return;
[self release];
}
#end
This is file "Progress_ToolViewController .h":
#import <UIKit/UIKit.h>
#class Progress;
#interface Progress_ToolViewController : UIViewController <UIAlertViewDelegate>{
BOOL userDismissed;
Progress *prog;
}
- (void)showAlert;
#end
This is file "Progress_ToolViewController.m":
#import "Progress_ToolViewController.h"
#import "Progress.h"
#implementation Progress_ToolViewController
- (void)showAlert
{
prog = [[Progress alloc] initWithTitle:#"Your Value:" message:#" " delegate:self cancelButtonTitle:nil okButtonTitle:nil];
prog.tag = 2;
userDismissed = NO;
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:#selector(dismissAlert) userInfo:nil repeats:NO];
[prog show];
}
-(void)dismissAlert{
if(userDismissed) return;
[prog dismissWithClickedButtonIndex:0 animated:YES];
[prog release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)alertView:(Progress *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([alertView tag] == 1) {
if (buttonIndex == 1) {
UIAlertView *subAlert = [[UIAlertView alloc] initWithTitle:#"Sub-Alert" message:#"You've chosen \"Sure\"." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[subAlert show];
[subAlert release];
}
else if (buttonIndex == 2) {
UIAlertView *subAlert = [[UIAlertView alloc] initWithTitle:#"Sub-Alert" message:#"You've chosen \"Not sure\"." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[subAlert show];
[subAlert release];
}
}
else if ([alertView tag] == 2) {
userDismissed = YES;
}
}
- (void)dealloc {
[super dealloc];
}
#end
When i run this app, it's also dismiss but it turns off the program. How can i solve it?
-(void)showAlert
{
prog = [[UIAlertView alloc]initWithTitle:#"Auto alert" message:#"" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:#selector(dismissAlert) userInfo:nil repeats:NO];
[prog show];
}
-(void)dismissAlert
{
[prog dismissWithClickedButtonIndex:0 animated:YES];
TrackingSetting *_obj = [[TrackingSetting alloc]init];
[self presentViewController:_obj animated:YES completion:nil];
}
Try this sample code it is working fine i have tested and after diss miss of alert you can create another view object and and show over it no issue.
But in your case i think "prog" is a view .so you have to remove that view from superview means it depends on how you are showing that view if you are adding that view using add sub view you have to remove from super view .if you are using present modal view then you have to dismiss that view .
I have 2 instances of UITextField and 1 instance of UIPickerView. UIPickerView is able to update data into
UITextField in didSelectRow:(NSInteger)row inComponent:(NSInteger)component. But how do I update 2 UITextField from UIPickerView when the user touch the UITextView?
Thanks. :)
you will need to make the UIPickerView the inputView of the textfields, i have made an example.
.h file:
#interface aViewController : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource> {
UITextField * textField0;
UITextField * textField1;
UIPickerView * pickerView;
UIToolbar * toolBar;
NSArray * items0;
NSArray * items1;
}
-(void)cancel;
-(void)close;
#end
.m need something like the following:
- (void)viewDidLoad
{
[super viewDidLoad];
textField0 = [[UITextField alloc] initWithFrame:CGRectMake(10.f, 20.0f, 100.f, 30.f)];
textField1 = [[UITextField alloc] initWithFrame:CGRectMake(130.f, 20.0f, 100.f, 30.f)];
textField0.backgroundColor = [UIColor grayColor];
textField1.backgroundColor = [UIColor grayColor];
pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320.f, 200.f)];
// Do any additional setup after loading the view from its nib.
[self.view addSubview:textField0];
[self.view addSubview:textField1];
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320.f, 44.f)];
UIBarButtonItem * cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancel)];
UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(close)];
toolBar.items = [NSArray arrayWithObjects:doneButton,cancelButton, nil];
items0 = [[NSArray alloc] initWithObjects:#"dogs",#"cats",#"llamas",#"emus", nil];
items1 = [[NSArray alloc] initWithObjects:#"bone",#"catnip",#"hay", nil];
[doneButton release];
[cancelButton release];
pickerView.showsSelectionIndicator = YES;
pickerView.delegate = self;
pickerView.dataSource = self;
textField1.inputView = pickerView;
textField0.inputView = pickerView;
textField1.inputAccessoryView = toolBar;
textField0.inputAccessoryView = toolBar;
}
#pragma mark - actions
-(void)cancel
{
//reset the values to the original values on cancel...
//do that here.
[textField0 resignFirstResponder];
[textField1 resignFirstResponder];
}
-(void)close
{
textField0.text = [items0 objectAtIndex:[pickerView selectedRowInComponent:0]] ;
textField1.text = [items1 objectAtIndex:[pickerView selectedRowInComponent:1]] ;
[textField0 resignFirstResponder];
[textField1 resignFirstResponder];
}
#pragma mark - pickerview handling
-(float)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
return 130.f;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component==0) {
return [items0 objectAtIndex:row];
}
return [items1 objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component==0) {
textField0.text = [items0 objectAtIndex:row];
return;
}
textField1.text = [items1 objectAtIndex:row];
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component ==0) {
return [items0 count];
}
return [items1 count];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
I haven't included any clean-up code.
Hello
I am having a alertview and i want to change the size by cgrectmake property but it does not happen .
It just take the by default size.
i tried following code.
- (void)viewDidLoad {
[super viewDidLoad];
UIAlertView* find = [[[UIAlertView alloc] init]initWithFrame:CGRectMake(0,0,300,200)];
[find setDelegate:self];
[find setTitle:#"Sample Alert"];
[find setNeedsLayout];
[find show];
[find release];
}
Thanks in advance .
To achieve what you want, in your code, after:
[find show];
add:
find.frame = CGRectMake(0,0,300,200);
It's not pretty though, I suggest you use ActionSheets.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Confirmation" message:#" Submit the answer " delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
[alert show];
[alert release];
UILabel *theTitle = [alert valueForKey:#"_titleLabel"];
[theTitle setTextColor:[UIColor blackColor]];
UILabel *theBody = [alert valueForKey:#"_bodyTextLabel"];
[theBody setTextColor:[UIColor blackColor]];
UIImage *theImage = [UIImage imageNamed:#"blue-white-abstract-background.jpg"];
theImage = [theImage stretchableImageWithLeftCapWidth:10 topCapHeight:10];
CGSize theSize = [alert frame].size;
UIGraphicsBeginImageContext(theSize);
[theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];
theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//[[alert layer] setContents:[theImage CGImage]];
[[alert layer] setContents:[UIColor clearColor]];
This code does a lot of things to alertview and modify it to increase the size of the alert view.
This does the job pretty well, and does not look weird when the alert appear (ahmet emrah solution has a pretty bad side effect).
CGAffineTransform myTransform = CGAffineTransformMakeScale(1.0, 0.5f);
[alert setTransform:myTransform];
You can subclass the UIAlertView. I have done something like this, change it for your need.
Header file,
#import <Foundation/Foundation.h>
/* An alert view with a textfield to input text. */
#interface AlertPrompt : UIAlertView
{
UITextField *textField;
}
#property (nonatomic, retain) UITextField *textField;
#property (readonly) NSString *enteredText;
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okButtonTitle;
#end
Source code,
#import "AlertPrompt.h"
#implementation AlertPrompt
static const float kTextFieldHeight = 25.0;
static const float kTextFieldWidth = 100.0;
#synthesize textField;
#synthesize enteredText;
- (void) drawRect:(CGRect)rect {
[super drawRect:rect];
CGRect labelFrame;
NSArray *views = [self subviews];
for (UIView *view in views){
if ([view isKindOfClass:[UILabel class]]) {
labelFrame = view.frame;
} else {
view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y + kTextFieldHeight , view.frame.size.width, view.frame.size.height);
}
}
CGRect myFrame = self.frame;
self.textField.frame = CGRectMake(95, labelFrame.origin.y+labelFrame.size.height + 5.0, kTextFieldWidth, kTextFieldHeight);
self.frame = CGRectMake(myFrame.origin.x, myFrame.origin.y, myFrame.size.width, myFrame.size.height + kTextFieldHeight);
}
- (id)initWithTitle:(NSString *)title 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])
{
// add the text field here, so that customizable from outside. But set the frame in drawRect.
self.textField = [[UITextField alloc] init];
[self.textField setBackgroundColor:[UIColor whiteColor]];
[self addSubview: self.textField];
// CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 20.0);
// [self setTransform:translate];
}
return self;
}
- (void)show
{
[textField becomeFirstResponder];
[super show];
}
- (NSString *)enteredText
{
return textField.text;
}
- (void)dealloc
{
[textField release];
[super dealloc];
}
#end
Hey guys, I'm having trouble obtaining the value/text from a AlertView TextField. Maybe someone can look at my code and see what I'm doing wrong. Every time I press OK, the label get's a (null) value. I must be missing something here.
TextFieldINAlertViewController.h
#import <UIKit/UIKit.h>
#interface TextFieldInAlertViewController : UIViewController {
UITextField *myTextField;
IBOutlet UILabel *labelView;
NSString *FieldStr1;
}
#property (nonatomic, copy) NSString *FieldStr1;
#end
TextFieldInAlertViewController.m
#import "TextFieldInAlertViewController.h"
#implementation TextFieldInAlertViewController
#synthesize FieldStr1;
- (void)viewDidLoad {
[super viewDidLoad];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter Name Here" message:#"blank" delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:#"OK!", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);
[alert setTransform:myTransform];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
[alert release];
}
-(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)index
{
if (index == 0){
return;
}
if (index == 1){
FieldStr1 = myTextField.text;
labelView.text = [NSString stringWithFormat:#"%#" , FieldStr1 ];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[super dealloc];
}
#end
myTextField is a local variable in viewDidLoad, but it's also a member variable in your class. Get rid of the local declaration, and you should be fine. Also, I'd move this code to viewDidAppear:, rather than viewDidLoad.
Since iOS5 you can use property on the alertView which is a much simplier way to add a textField.
If you want to add a TextField to an UIAlertView, you can use this property (alertViewStyle) for UIAlertView:
- (void)showAlert{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
((UITextField *) [alertView textFieldAtIndex:0]).text = #"Default Value";
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(#"%#", [alertView textFieldAtIndex:0].text);
}
Check UIAlertView reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html
Hugues