Getting three warning messages for these three statements
ActionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[ActionSheet showInView:self.view ];
[ActionSheet release];
Local declaration of 'ActionSheet' hides instance variable
#property (nonatomic, retain) UIActionSheet *ActionSheet;
#synthesize ActionSheet;
-(void)displayActionSheet:(id)sender
{
UIActionSheet *ActionSheet = [[UIActionSheet alloc]
initWithTitle:#"Language Options"
delegate:nil
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Devanagari", #"English", nil];
ActionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[ActionSheet showInView:self.view ];
[ActionSheet release];
}
Any idea what is wrong.
First: variables are NOT written in uppercase. please rename your actionSheet variable.
Second: your property hast the same name, as your local variable (ActionSheet in your case). If you want to save the actionSheet in the member variable, than remove UIActionSheet* resulting in that function:
-(void)displayActionSheet:(id)sender
{
ActionSheet = [[UIActionSheet alloc]
initWithTitle:#"Language Options"
delegate:nil
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Devanagari", #"English", nil];
ActionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[ActionSheet showInView:self.view ];
[ActionSheet release];
}
Related
I'm adding UIActionSheet with 100 sheets to UIView.
Here is a code.
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:#"Choose Number"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
for (int i = 1; i <= 100; i++) {
[actionSheet addButtonWithTitle:[NSString stringWithFormat:#"%d", i]];
}
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:#"Cancel"];
cancelIndex = actionSheet.cancelButtonIndex;
[actionSheet showInView:self.view];
Problem Image
The line between 22 and 23 overlaps.
What is problem?
#define kCustomAlert #"UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Alert Back" message:msg delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];[alert show];"
How to call this alert in view controller class?
Declare this macro in your pch file:
#define kCustomAlert() [[[UIAlertView alloc] initWithTitle:#"Alert Title" message:#"Alert Message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] show]
Macro Call: kCustomAlert();
Alert Macro With Parameter:
#define kCustomAlertWithParam(title,msg) [[[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] show]
kCustomAlertWithParam(#"This is Title",#"This is message");
Alert Macro with Parameters and Target(For the use of: UIAlertView Delegate Methods)
Please set UIAlertViewDelegate for your Controller.
#define kCustomAlertWithParamAndTarget(title,msg,target) [[[UIAlertView alloc] initWithTitle:title message:msg delegate:target cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] show]
kCustomAlertWithParamAndTarget(#"This is Title",#"This is message",self);
You need to make a macro function You can't define it like so. Your Syntax is wrong.
Do it in this way:
#define ShowAlert() [[[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] show]
And call it like:
ShowAlert();
You can also pass the parameters:-
#define ShowAlert(myTitle, myMessage) [[[UIAlertView alloc] initWithTitle:myTitle message:myMessage delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] show]
And call it like:
ShowAlert(#"YourTitle", #"YourMessage");
Note: I'm not saying that this is good to use just telling the way to do so.
I don't know how to achieve this or is it possible even or not but the alternative for this is to use below method in your AppDelegate.m and declare the method in AppDelegate.h file and then you can call it by creating AppDelegate instance in any class
-(void)showAlertWithTitle:(NSString *)title message:(NSString *)message
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
I know it's a old question, but as UIAlertView is now deprecated, the answers provided will generate a warning.
Here's how you can achieve this with UIAlertViewController :
FROM A VIEW CONTROLLER
#define ShowAlert(title, myMessage) { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:myMessage preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:nil]]; [self presentViewController:alertController animated:YES completion:nil]; }
Note that this one can only be used in a view controller.
FROM ANYWHERE
If you want to be able to show an alert anywhere, you'll need this one :
#define ShowAlertFromTopMostController(title, myMessage) { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:myMessage preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:nil]]; [[Utils topMostController] presentViewController:alertController animated:YES completion:nil]; }
and you'll need to add following method to a Utils class (subclassing NSObject) :
+(UIViewController*) topMostController {
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
return topController;
}
How to assign the alert message textfield input value to the button?
In the below code, I am assigning value to textfield but requirement is assign value to button instead of textfeild through alert message help me to solve the problem in iPhone.
-(IBAction)alertmessage{
[self setingValue];
}
-(void)setingValue{
myAlert = [[UIAlertView alloc] initWithTitle:#"Enter year" message:#"alert message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
[myAlert addTextFieldWithValue:#"" label:#"entervalue"];
alertTextField=[myAlert textFieldAtIndex:0];
alertTextField.keyboardType=UIKeyboardTypeAlphabet;
alertTextField.clearsOnBeginEditing=YES;
alertTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
alertTextField.keyboardAppearance=UIKeyboardAppearanceAlert;
[myAlert show];
[myAlert release];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Ok"]) {
yearSelection.text= alertTextField.text;
}
}
if i enter keyboard value from alert textfield as input type that has to assign or appear on the button.
-(void)setingValue{
myAlert = [[UIAlertView alloc] initWithTitle:#"Enter year" message:#"alert message"
delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
alertTextField=[UITextfiled alloc]initwithFrame:CGRectFrame:(5,25,200,35)]; //set the frame as you need.
alertTextField.Placeholder=#"Enter Value";
alertTextField.keyboardType=UIKeyboardTypeAlphabet;
alertTextField.clearsOnBeginEditing=YES;
alertTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
alertTextField.keyboardAppearance=UIKeyboardAppearanceAlert;
[myAlert addSubview:alertTextFiedl];
[alertTextField release];
[myAlert show];
[myAlert release];
}
I am using FBConnect in my app. The log in action sheet buttons are title "Log in Facebook" and "LogOut Facebook" but I want to display "Log into Facebook" and "Publish to Facebook". Currently, it looks like this...
alt text http://freezpic.com/pics/6944f45f17ba4bbb8220637d5a00a1c6.jpg
...but I want it to look like this...
alt text http://www.freezpic.com/pics/93f28f4f9103f0842c849d7daa644f81.jpg
... possibly set in these methods:
- (void)session:(FBSession*)session didLogin:(FBUID)uid {
//Show button log out
}
- (void)sessionDidLogout:(FBSession*)session {
//show button log in
}
Edit01- Alert sheet code from answer comment:
-(IBAction)mySheet:(id)sender {
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"FaceBook"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Share On the Facebook" ,
#"Log in Facebook" ,
#"LogOut Facebook" ,nil];
[menu showInView:self.view];
[menu release];
}
Sure, just show a different UIActionSheet with just those two buttons depending on the state of the Facebook connection.
What about:
-(IBAction)mySheet:(id)sender
{
if (alreadyLoggedInToFacebook) {
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"FaceBook"
delegate:self cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles: #"Share On the Facebook" , #"Log in Facebook" ,
#"LogOut Facebook" ,nil];
} else {
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"FaceBook"
delegate:self cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles: #"LogOut Facebook" ,nil];
}
[menu showInView:self.view];
[menu release];
}
Finally i implement that ! (alreadyLoggedInToFacebook) must be (season.isConnect) . every thing is good ! but still a problem . after login - logout and share show great but didn't work great ! it means if user tap Logout button , login window appears again ! why ? i think , its because of FBLoginButton , when delet this method my UIActionSheet doesn't Show
! here is my code :
-(IBAction)mySheet:(id)sender
{
if (session.isConnected) {
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"FaceBook"
delegate:self cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles: #"Share On the Facebook" , #"Log out Facebook" ,nil];
[menu showInView:self.view];
[menu release];
} else {
UIActionSheet *menu2 = [[UIActionSheet alloc] initWithTitle:#"FaceBook"
delegate:self cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles: #"Log in Facebook" ,
nil];
[menu2 showInView:self.view];
[menu2 release];
}
}
- (void)actionSheet:(UIActionSheet *)menu2 didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [menu2 cancelButtonIndex])
{
FBLoginDialog* login = [[FBLoginDialog alloc] initWithSession:session];
[login show];
[login release];
}
}
- (void)actionSheet:(UIActionSheet *)menu didDismissWithButtonIndex2:(NSInteger)buttonIndex {
if (buttonIndex != [menu cancelButtonIndex])
{
[session logout];
}
}
Is it possible to show an alertview with a textbox inside like the AppStore app.
It asks for password in such a dialog.
I've seen atleast a couple of other third party apps using it. Is it a private API?
Here's an "Apple Approved" way of doing it from Tharindu Madushana. I got it from his comment in this page: http://iosdevelopertips.com/undocumented/alert-with-textfields.html
// Ask for Username and password.
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:#"Twitter Details!" message:#"\n \n \n" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
// Adds a username Field
UITextField *utextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
utextfield.placeholder = #"Username";
[utextfield setBackgroundColor:[UIColor whiteColor]];
[alertview addSubview:utextfield];
// Adds a password Field
UITextField *ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 80.0, 260.0, 25.0)];
ptextfield.placeholder = #"Password";
[ptextfield setSecureTextEntry:YES];
[ptextfield setBackgroundColor:[UIColor whiteColor]]; [alertview addSubview:ptextfield];
// Move a little to show up the keyboard
CGAffineTransform transform = CGAffineTransformMakeTranslation(0.0, 80.0);
[alertview setTransform:transform];
// Show alert on screen.
[alertview show];
[alertview release];
//...
// Don't forget to release these after getting their values
[utextfield release];
[ptextfield release];
And finally to get the text back
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
return; //Cancel
UITextField *field = (UITextField *)[[alertView subviews] lastObject];
NSLog (#"%#", field.text);
}
Yes, it's undocumented. To add a text field to UIAlertView, use addTextFieldWithValue: label: method. You call with the default text as the first argument and the text that displays in an empty field as the second. Having done that, you can access the field via using textFieldAtIndex:n - see below.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Who are you?"
message:#"Give your full name"
delegate:self cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[alert addTextFieldWithValue:#""label:#"Name"];
// Customise name field
UITextField* name = [alert textFieldAtIndex:0];
name.clearButtonMode = UITextFieldViewModeWhileEditing;
name.keyboardType = UIKeyboardTypeAlphabet;
name.keyboardAppearance = UIKeyboardAppearanceAlert;
[alert show];
The next snippet shows how to retrieve the value in the name field:
NSLog("Name is %#", [[modalView textFieldAtIndex:0] text]);
This is a really old questions with really old answers.
This is a sample of how I get a UITextfield into a UIAlertView since ios 5:
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"New List Name" message:#"" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"Continue", nil];
message.alertViewStyle = UIAlertViewStylePlainTextInput;
self.alertTextField = [message textFieldAtIndex:0];
self.alertTextField.keyboardType = UIKeyboardTypeAlphabet;
message.delegate = self;
[message show];
[self.alertTextField becomeFirstResponder];
where alertTextField was set up like this:
#property (nonatomic, strong) UITextField *alertTextField;
Jeff Lamarche posted some sample code on his blog to do just this. The formatting looked a bit wonky when I tried it but it's probably a good starting point.