How can I get a value from UITextField in UIAlertView? - objective-c

I have Used in cocos2d game engine. this is my code:
-(void)timed1: (id)sender {
UIAlertView *dialog = [[[UIAlertView alloc] init] retain];
[dialog setDelegate:self];
[dialog setTitle:#"Enter Time:"];
[dialog setMessage:#" "];
UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[dialog addSubview:nameField];
[nameField setBackgroundColor:[UIColor whiteColor]];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 70.0);
[dialog setTransform: moveUp];
[dialog addButtonWithTitle:#"Done"];
[dialog addButtonWithTitle:#"Cancel"];
nameField.clearButtonMode = UITextFieldViewModeWhileEditing;
nameField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
//timeStatus is a int type global variable //
timeStatus =[nameField.text intValue]; //** this line not working**//
[nameField release];
[dialog show];
}

The reason you cannot get a value is that the function will exit BEFORE the user types anything! When you call [dialog show] the dialog is displayed to the user where they may then enter text.
You should wait until the user selects a button, which will trigger your delegate function, and THEN access the text field property.
Note to do this you will either need to retain a pointer to the UITextField object you added (easiest), or find it again amongst the subviews and cast it.

Release nameField later, and get the text property from there.

Related

textField does not show in the alertView

Here is my code:
UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:#"Alert" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
UITextField *textField = [[UITextField alloc] initWithFrame: CGRectMake(12, 45, 260, 30)];
[textField setBackgroundColor: [UIColor clearColor]];
textField.borderStyle = UITextBorderStyleRoundedRect;
[theAlert addSubview:textField];
[theAlert show];
[textField release];
[theAlert release];
There is no textFiled in alertView, only a title "Alert" and a button "OK"
I will refer you the Apple Documentation in regards to UIAlertView Class Reference. Specifically
Subclassing Notes
The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified
So what this means is you can't add subviews to an instance of UIAlertView the recent UIAlertView still has the addSubview: method is because UIAlertView is a subclass of UIView which has this method, but as of iOS7 the method addSubview: for UIAlertView no longer calls the super method on UIView it just does nothing.
So basically what you are after is not possible.
There are however alertViewStyles that you can use like UIAlertViewStylePlainTextInput which will add a single UITextField to the UIAlertView which you can then access using the method textFieldAtIndex:. However please be aware that you can't really do anything with this again because it is part of the UIAlertViews hierarchy so again it is made to be used as is.
Messing with the UIAlertView hierarchy will get your app rejected from the Apple Review process under
2.5 - Apps that use non-public APIs will be rejected
your background color is clear so It's not showing
[textField setBackgroundColor: [UIColor redColor]];.
and also show default text field
theAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
//or for username and password
theAlert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
// or for password
theAlert.alertViewStyle = UIAlertViewStyleSecureTextInput;
Try this
You can use the default alertView with textfield.For that you need to set the alertViewStyle
[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
Here message is object of alertView.
You can get the textfield value using the delegate method as shown below
- (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];
NSLog(#"Username: %#\nPassword: %#", username.text, password.text);
}
}
Or you can set alert.alertViewStyle = UIAlertViewStylePlainTextInput;
This will add a text field for you. You can access it in the
UIAlertView delegate callback by using
UITextField *textField = [alertView textFieldAtIndex:0];
If your using ios 7 then add below code to your set of code
theAlert.alertViewStyle=UIAlertViewStylePlainTextInput;
Full code
UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:#"Alert" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
UITextField *textField = [[UITextField alloc] initWithFrame: CGRectMake(12, 45, 260, 30)];
[textField setBackgroundColor: [UIColor greenColor]];
textField.borderStyle = UITextBorderStyleRoundedRect;
theAlert.alertViewStyle=UIAlertViewStylePlainTextInput;
[theAlert show];

UIActionSheet comes up halfway

I was looking for a way to make a UIPickerView show a "Done", "Cancel" buttons.
Googling around I found good posts, all of them had UIActionSheet involved.
So I did that
container is a UIView
actionSheet is a UIActinSheet
pickerView is a UIPickerView
in viewDidLoad:
container = [[[UIView alloc] initWithFrame:CGRectMake(rect.origin.x,rect.origin.y, rect.size.width, rect.size.height)] autorelease];
[self.view addSubview:container];
actionSheet = [[UIActionSheet alloc] initWithTitle:#"myTitle"
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
pickerView = [[[UIPickerView alloc] initWithFrame:pickerFrame]autorelease];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
UIToolbar *toolbar = [[[UIToolbar alloc] init] autorelease];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[[NSMutableArray alloc] init] autorelease];
[items addObject:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(dismissActionSheet)] autorelease]];
[items addObject:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:#selector(goActionSheet)] autorelease]];
[items addObject:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(goActionSheet)] autorelease]];
[toolbar setItems:items animated:NO];
[actionSheet addSubview:toolbar];
[actionSheet addSubview:pickerView];
[actionSheet showInView:container];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
The app starts great, and the whole thing shows up nice as supposed to be on the line: [actionSheet showInView:container];
Now, When I try to dismiss the ActionSheet, it works great!
in dismissActionSheet:
[actionSheet dismissWithClickedButtonIndex:1 animated:NO];
The problem is, how do I make it show up again?
When I use:
[actionSheet showInView:container];
I only get the Toolbar picking from the bottom with the buttons and that's it. The whole PickerView is at the bottom and I can't see it.
What am I doing wrong here?
Action sheets are intended to be used more like alerts, in that they are standalone "make a choice now" presenters. They do normally only display part-way on the iPhone.
Your picker should be independent of the action sheet. If you want to have "Done" and "Cancel" buttons perhaps you would want to consider adding them to the picker's navigation bar?
You can use UIToolBar having Done and Cancel Button, with your PickerView, this will be easy.

Custom AlertView With Background

Everybody, I need to set one image on UIAlertView..
I have attached my UIAlertview with image prob..
i have used this code lines..
UIAlertView *theAlert = [[[UIAlertView alloc] initWithTitle:#"Atention"
message: #"YOUR MESSAGE HERE", nil)
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] autorelease];
[theAlert show];
UILabel *theTitle = [theAlert valueForKey:#"_titleLabel"];
[theTitle setTextColor:[UIColor redColor]];
UILabel *theBody = [theAlert valueForKey:#"_bodyTextLabel"];
[theBody setTextColor:[UIColor blueColor]];
UIImage *theImage = [UIImage imageNamed:#"Background.png"];
theImage = [theImage stretchableImageWithLeftCapWidth:16 topCapHeight:16];
CGSize theSize = [theAlert frame].size;
UIGraphicsBeginImageContext(theSize);
[theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];
theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[theAlert layer] setContents:[theImage CGImage]];
please solve this issue..
i need only image with alert..
Try this...
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"UIAlert View" message:#"hello" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Close",nil];
UIImage *alertImage = [UIImage imageNamed:#"plus.png"];
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:alertImage];
backgroundImageView.frame = CGRectMake(0, 0, 282, 130);
backgroundImageView.contentMode = UIViewContentModeScaleToFill;
[alert addSubview:backgroundImageView];
[alert sendSubviewToBack:backgroundImageView];
[alert show];
[alert release];
You should consider to not use UIAlertView, but have your own AlertView see TSAlertView for an alternative implementation, that is not derived from UIAlertView.
TSAlertView is allowing you to set your own background image.
Another solution that I am not recommending could be:
You can use introspection: Loop over the UIAlertViews subviews, identify the one that holds the background image set it hidden and place your own backroundimage at an index below/over the original image view.
I found this project: Subclass UIAlertView to customize the look of an alert. It is not working for iOS 4.2+, but with my introspection idea you can make it work again:
change the -layoutSubviews of JKCustomAlert to:
- (void) layoutSubviews {
for (UIView *v in [self subviews]) {
if ([v class] == [UIImageView class]) {
[v setHidden:YES];
}
}
alertTextLabel.transform = CGAffineTransformIdentity;
[alertTextLabel sizeToFit];
CGRect textRect = alertTextLabel.frame;
textRect.origin.x = (CGRectGetWidth(self.bounds) - CGRectGetWidth(textRect)) / 2;
textRect.origin.y = (CGRectGetHeight(self.bounds) - CGRectGetHeight(textRect)) / 2;
textRect.origin.y -= 30.0;
alertTextLabel.frame = textRect;
alertTextLabel.transform = CGAffineTransformMakeRotation(- M_PI * .08);
}
I am NOT promising, that this trick will work in future versions of iOS
A solution I like to use for this is to add a UIView to the ViewController, which mimics the appearance of an alert.
The other property of a UIAlertView is that no other part of the app can be used until the alert is dismissed. This can easily be mimicked by making your UIView a subview of another UIView (with a clear background), which takes up the entire screen.
If you don't want to implement that yourself, here's a Custom Alert View class you could use: https://github.com/shivsak/CustomAlertView

how can an UIAlertView button calls another function?

-(void)buPressed{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Game Over"
message:#"YOU LOST! ALL YOUR BASE ARE BELONG TO US!"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Publish", nil];
[alertView show];
[alertView release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex==0){
NSLog(#"%d",buttonIndex);
}
else{
[self bPressed];
}
}
-(void)bPressed{
ModalViewConroller *yeniSayfa=[[ModalViewConroller alloc] init];
yeniSayfa.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:yeniSayfa animated:YES];
[yeniSayfa release];
//Restore to Defaults
[button_1 setSelected:NO];
[button_2 setSelected:NO];
[button_3 setSelected:NO];
[button_4 setSelected:NO];
[button_5 setSelected:NO];
[button_6 setSelected:NO];
slider.value=50.00;
UIImage *image = [UIImage imageNamed:#"Smiley_00025.png"];
imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(81, 43, image.size.width, image.size.height);
[self.view addSubview:imageView];
}
This is my code i want to make the publish button to call bPressed function but it is giving a warning and the program crashes when i touch the publish button i want to open a modalview when i push the publish button can anybody help me?
You need to declare the function in your header file so that other objects (in this case an instance of UIAlertView, since its delegate is set to your class) know that this method exists.
So, in your whatever_class.h file, add the following line below the #interface{ }:
-(void)bPressed;

How can get the value of text field by a alert view using Cocos2d?

I am trying to develop a application by using Cocos2d. I can't getting value from textfield . How can get the value of text field (in a alert view) by using Cocos2d ?
-(void)timed1: (id)sender
{
UIAlertView* dialog = [[[UIAlertView alloc] init] retain];
[dialog setDelegate:self];
[dialog setTitle:#"Enter Time:"];
[dialog setMessage:#" "];
UITextField * nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[dialog addSubview:nameField];
[nameField setBackgroundColor:[UIColor whiteColor]];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 70.0);
[dialog setTransform: moveUp];
[dialog setBackgroundColor:[UIColor clearColor]];
[dialog addButtonWithTitle:#"Done"];
[dialog show];
nameField.clearButtonMode = UITextFieldViewModeWhileEditing;
nameField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
nameField.keyboardAppearance = UIKeyboardAppearanceAlert;
nameField.autocapitalizationType = UITextAutocapitalizationTypeWords;
// timeStatus is a int type global variable
timeStatus =[nameField.text intValue]; // this line not working i can't getting value namefield
[dialog release];
[nameField release];
}
Action sheets and alerts are handled asynchronously. In your case, the message [dialog show] merely schedules the show event for some later execution (handled by the main run loop). If you put in a few NSLog()s, you'll see that the [show] message returns almost immediately, at which point your user hasn't entered any data, nameField's text is blank, and this converts to an int of 0.
You want a blocking, modal dialog with input -- action sheets and alerts are not designed for user input beyond yes/no/cancel button pushes. You'll have to cook up a view of your own: not too hard, but it will require more work than using an action sheet/alert.