I want to display an alert view with a search bar option on it. How can I create a search bar in a alert view? Any sample codes would be helpful.
I have created a search bar and and added the text box as its subview.
UIAlertView * myAlertView = [[UIAlertView alloc] initWithTitle:#" "
message:#" "
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil, nil];
UISearchBar *myTextField = [[UISearchBar alloc] initWithFrame:CGRectMake(30.0, 35.0, 180.0, 35.0)];
UIButton *searchbtn = [[UIButton alloc] initWithFrame:CGRectMake(230.0, 35.0, 40.0, 35.0)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[myAlertView setBackgroundColor:[UIColor grayColor]];
[searchbtn setBackgroundColor:[UIColor grayColor]];
[myAlertView addSubview:myTextField];
[myAlertView addSubview:searchbtn];
If there is any other better way please let me know.
There is a better way:
UIAlertView *myAlertView = [[[UIAlertView alloc] initWithTitle:#"Keyword Search"
message:#""
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Search", nil] autorelease];
myAlertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[myAlertView show];
And in the Delegate:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
NSString *searchTerm = [alertView textFieldAtIndex:0].text;
}
Related
i have one button in the lockscreen to display a alert when is touched, i am just playing with this stuff and tried to do this, but when i touch the button it crash and respring to enter in safe mode. I am running it with theos in my iPhone.Here is my code:
#import <UIKit/UIKit.h>
#interface SBAwayView : UIView
#end
#interface SBAwayController
UIButton *myButton;
-(void)unlockWithSound : (BOOL)sound;
#end
%hook SBAwayController
-(id)lock{
SBAwayView *v = MSHookIvar<id>(self, "_awayView");
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(21, 80, 100, 35);
[myButton setTitle:#"My Button" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(myButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[v addSubview:myButton];
%orig;
}
-(void)myButtonPressed{
UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:#"Title of Alert" message:#"Message of Alert" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[theAlert show];
[theAlert release];
}
%end
First try to debug and see function gets called or not.
then also its crashing then try to use autorelease
UIAlertView *theAlert = [[[UIAlertView alloc] initWithTitle:#"Title of Alert" message:#"Message of Alert" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil] autorelease] ;
[theAlert show];
You needed %new(v#:) before your void;
%new(v#:)
-(void)myButtonPressed{
UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:#"Title of Alert" message:#"Message of Alert" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[theAlert show];
[theAlert release];
}
I have an UIAlertView with UITextView.
on ViewDidAppear I do [textView setText:] with a large text, but the alert shows an empty textView, and only after I touch the textView to scroll, the text appears.
What should I do in order to make the text appear in the textView in the alert, without scrolling it to "refresh" it?
Thanks!
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
av = [[UIAlertView alloc]initWithTitle:#"Terms of Service" message:#"\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:#"Disagree" otherButtonTitles:#"Agree",nil];
UITextView *myTextView = [[UITextView alloc] initWithFrame:CGRectMake(12, 50, 260, 142)];
[myTextView setTextAlignment:UITextAlignmentCenter];
[myTextView setEditable:NO];
myTextView.layer.borderWidth = 2.0f;
myTextView.layer.borderColor = [[UIColor darkGrayColor] CGColor];
myTextView.layer.cornerRadius = 13;
myTextView.clipsToBounds = YES ;
[myTextView setText:#"LONG LONG TEXT"];
[av addSubview:myTextView];
[myTextView release];
[av setTag:1];
[av show];
}
This is because you have initially set message as #"\n\n\n\n\n\n\n" for UIAlertView. Set your UITextView first and then set UIAlertView's message as textView's text.
Add \n characters in place of message in alert view . Then create a UILabel add add to alert view. Like this
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:#"%#\n\n\n", #"Title"] message:#"\n" delegate:self cancelButtonTitle:NEVER_DISPLAY_BUTTON_TEXT otherButtonTitles:nil];
[alert addButtonWithTitle:#"Text"];
[alert addButtonWithTitle:#"Text"];
[alert addButtonWithTitle:#"Text"];
[alert addButtonWithTitle:#"Text"];
[alert show];
newTitle = [[UILabel alloc] initWithFrame:CGRectMake(10,-55,252,230)];
newTitle.numberOfLines = 0;
newTitle.font = [UIFont systemFontOfSize:15];
newTitle.textAlignment = UITextAlignmentCenter;
newTitle.backgroundColor = [UIColor clearColor];
newTitle.textColor = [UIColor whiteColor];
newTitle.text = [NSString stringWithFormat:#"%#",self.message];
[alert addSubview:newTitle];
You might need to adjust size of Uilable to match in your alert view.
Try with this:
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"Terms of Service"
message:[NSString stringWithFormat:#"%# \n\n\n",myTextView.text]
delegate:self
cancelButtonTitle:#"Disagree"
otherButtonTitles:#"Agree",nil
];
I need to add a TextField to an UIAlertView. I understand that apple discourage this approach. So is there any library that i could make use of to add a TextField to a UIAlertView look-alike frame ?
For iOS5:
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"Title" message:#"Please enter someth" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
av.alertViewStyle = UIAlertViewStylePlainTextInput;
[av textFieldAtIndex:0].delegate = self;
[av show];
Also, you 'll need to implement UITextFieldDelegate, UIAlertViewDelegate protocols.
Unfortunately the only official API for this is iOS 5 and up, it's a property called alertViewStyle which can be set to the following parameters:
UIAlertViewStyleDefault
UIAlertViewStyleSecureTextInput
UIAlertViewStylePlainTextInput
UIAlertViewStyleLoginAndPasswordInput
UIAlertViewStylePlainTextInput being the one you want.
Messing with the view hierarchy as described above is strongly discouraged by Apple.
I'm using BlockAlertsAndActionSheets instead of the Apple components for AlertViews and ActionSheets as I prefer the blocks-approach. Also contains a BlockTextPromptAlertView in the source, which might be what you want. You can replace the images of that control to get the Apple-style back.
Project on gitgub
Tutorial which gets you started
Example:
- (IBAction)newFolder:(id)sender {
id selfDelegate = self;
UITextField *textField;
BlockTextPromptAlertView *alert = [BlockTextPromptAlertView promptWithTitle :#"New Folder"
message :#"Please enter the name of the new folder!"
textField :&textField];
[alert setCancelButtonWithTitle:#"Cancel" block:nil];
[alert addButtonWithTitle:#"Okay" block:^{
[selfDelegate createFolder:textField.text];
}];
[alert show];
}
- (void)createFolder:(NSString*)folderName {
// do stuff
}
Try something like this:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Title"
message:#"\n\n"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Save", nil] autorelease];
CGRect rect = {12, 60, 260, 25};
UITextField *dirField = [[[UITextField alloc] initWithFrame:rect] autorelease];
dirField.backgroundColor = [UIColor whiteColor];
[dirField becomeFirstResponder];
[alert addSubview:dirField];
[alert show];
As of iOS 8 UIAlertView has been deprecated in favor of UIAlertController, which adds support for adding UITextFields using the method:
- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;
See this answer for an example.
You can try:
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Your title here!" message:#"this gets covered" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[myAlertView addSubview:testTextField];
[myAlertView show];
[myAlertView release];
Follow this link for detail.
first of All Add UIAlertViewDelegate into ViewController.h File like
#import <UIKit/UIKit.h>
#interface UIViewController : UITableViewController<UIAlertViewDelegate>
#end
and than Add Below Code where you wants to alert Display,
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Message"
delegate:self
cancelButtonTitle:#"Done"
otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
and it's delegate method which returns what input of UItextField
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"%#", [alertView textFieldAtIndex:0].text);
}
adding to answer from 'Shmidt', the code to capture text entered in UIAlertView is pasted below (thank you 'Wayne Hartman' Getting text from UIAlertView)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
self.userNumber = [alertView textFieldAtIndex:0].text;
if (self.userNumber) {
// user enetered value
NSLog(#"self.userNumber: %#",self.userNumber);
} else {
NSLog(#"null");
}
}
}
refer this... http://iosdevelopertips.com/undocumented/alert-with-textfields.html this is private api and if you use it for app in appstore it might get rejected but it is fine for enterprise development.
Here is the code I have to create an UIAlertView with a textbox.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter A Username Here" message:#"this gets covered!"
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];
alert.tag = kAlertSaveScore;
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
[alert release];
[myTextField release];
My question is, how do I get the value from the textfield in:
- (void) alertView:(UIAlertView *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
}
I know I can get the standard stuff for the alertview such as actionSheet.tag and such, but how would I get the above created textfield?
#interface MyClass {
UITextField *alertTextField;
}
#end
And instead of declaring it locally, just use that.
//...
alertTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
//...
- (void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *text = alertTextField.text;
alertTextField = nil;
}
Just give it a tag, and find it using the tag later. So, using your code:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter A Username Here" message:#"this gets covered!"
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];
alert.tag = kAlertSaveScore;
// Give the text field some unique tag
[myTextField setTag:10250];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
[alert release];
[myTextField release];
Then, in the call-back, wherever that happens to be and without having to worry about memory management or state management of the text field:
- (void) alertView:(UIAlertView *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Get the field you added to the alert view earlier (you should also
// probably validate that this field is there and that it is a UITextField but...)
UITextField* myField = (UITextField*)[actionSheet viewWithTag:10250];
NSLog(#"Entered text: %#", [myField text]);
}
I want to create a UIAlertView that will say that "...in progress". It will also show that UIActivityindicatorView on it. Could you let me know how can I do that?
Thanks.
Its pretty simple. Just create a UIActivityIndicatorView and add it as a subview to the UIAlertView.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#" " message:#" " delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[alert addSubview:progress];
[progress startAnimating];
[alert show];
In my case i found what using hardcoded frame origins it's bad. And if my message has more one line, indicator showing top of my message.
So i create function with layouting indicator if size UIAlertView
+(UIAlertView*) progressAlertWithTitle:(NSString*) title andMessage:(NSString*) message andDelegate:(id)delegate{
UIAlertView *progressAlert = [[UIAlertView alloc] init];
[progressAlert setTitle:title];
[progressAlert setMessage:message];
[progressAlert setDelegate:delegate];
UIActivityIndicatorView *progress=nil;
progress= [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[progressAlert addSubview:progress];
[progress startAnimating];
[progressAlert show];
progress.frame=CGRectMake(progressAlert.frame.size.width/2-progress.frame.size.width, progressAlert.frame.size.height-progress.frame.size.height*2, progress.frame.size.width, progress.frame.size.height);
return progressAlert;
}
In this case, indicator always by center
One line message:
More one line message:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.center = CGRectMake(xcords, ycords, width, height);
[alert addSubview:spinner];
[spinner startanimating];
[alert show];
This spinner gets hidden on dismiss of AlertView.
[alert dismissWithClickedButtonIndex:0 animated:YES];