I have a table view. When the user clicks any cell of the tableView I want to display both UIAlertView and UIActionSheet at the same time. The AlertView should be displayed above the ActionSheet. And if the user clicks the "Cancel" button in AlertView, both the UIActionSheet and the UIAlertView should disappear at the same time. How can I achieve this?
Follow this:-
UIActionSheet *actionSheetTitleFontStyle = [[UIActionSheet alloc] initWithTitle:#"\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:#"ok" destructiveButtonTitle:nil otherButtonTitles:nil];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hello" message:#"Hello" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[actionSheetTitleFontStyle addSubview:alert];
[alert show];
It may help you.Thanks :)
Related
I have a tableview of items and when i click one row, I want to use uialertview with uiactionsheet with button: edit, remove and cancel. When I click button edit, I will open a modal view. Before, I did edit modal view already and when I click one row, I will go to edit modal view, but now I want to add uiactionsheet, so how can I do this ?
Write UIAlertview or UIActionsheet in -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath method.
I think it will be helpful to you.
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"title" delegate:self cancelButtonTitle:#"cancel" destructiveButtonTitle:#"delete" otherButtonTitles:#"other 1", #"other 2", nil];
[actionSheet showInView:self.view];
//or
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"title" message:#"message" delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:#"button",#"button1", nil];
[alert show];
}
Just use a UIAlertView as follows:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Edit", #"Remove", nil];
[alert show];
The following method will check which button is pressed:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
switch (buttonIndex)
{
case 1:
// perform Edit
break;
case 2:
// perform Remove
break;
case 0:
// perform Cancel, if any
break;
}
}
I am trying to create a UIAlertView with three buttons (which will be stacked). I would like the Cancel button to be in the middle, between the two other buttons. I have tried setting the cancelButtonIndex to 1, but if there are two other buttons, it simply places them at indexes 0 and 1. I know I could just change the names of the buttons, but I want the darker blue formatting of the cancel button.
EDIT: **
Please note - I know how to get the three buttons with the titles in the correct order, but only if all three buttons essentially look like 'other' buttons; I want the cancel button to have the cancel button dark blue background so that it will look like a regular cancel button.
**
I've tried
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:button1Title,button2Title,nil] autorelease];
alert.cancelButtonIndex = 1;
[alert show];
and
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil] autorelease];
alert.cancelButtonIndex = 1;
[alert addButtonWithTitle:button1Title];
[alert addButtonWithTitle:button2Title];
[alert show];
and
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:addButtonWithTitle:button1Title,nil] autorelease];
alert.cancelButtonIndex = 1;
[alert addButtonWithTitle:button2Title];
[alert show];
to no avail. Is it even possible to accomplish what I am trying to do?
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:nil] autorelease];
[alert addButtonWithTitle:button1Title];
[alert addButtonWithTitle:#"Cancel"];
[alert addButtonWithTitle:button2Title];
[alert show];
Might Help,
Cheers.
I have two ancillary points to this answer.
1) While, to the best of my knowledge, Apple has not rejected an app for reasonable modification of a UIAlertView; They have said that the view hierarchy of classes like UIAlertView should be considered private.
2) This question is a good example of why you should ask a question more about your end goal rather than the steps to get there. The only reason I know what this question is about is as a result of a comment left at my answer here.
Answer:
Because of your comment I know that you are looking to create a UIAlertView that has stacked buttons even when there are only 2 buttons.
I find the most logical place for code like this is in a category. Since generally the code needed to manipulate the alert-view needs to be around the show call, I created a category method I call instead of show and the method in turn calls show itself.
-(void)showWithButtonsStacked{
static NSString *tempButtonTitle = #"SomeUnlikelyToBeUsedTitle";
BOOL willAddFakeButton = (self.numberOfButtons == 2); // Button are only side by side when there's 2
if (willAddFakeButton){
self.clipsToBounds = YES;
[self addButtonWithTitle:tempButtonTitle]; // add temp button so the alertview will stack
}
BOOL hasCancelButton = (self.cancelButtonIndex != -1); // If there is a cancel button we don't want to cut it off
[self show];
if (willAddFakeButton){
UIButton *cancelButton = nil;
UIButton *tempButton = nil;
for (UIButton *button in self.subviews) {
if ([button isKindOfClass:[UIButton class]]){
if (hasCancelButton && [button.titleLabel.text isEqualToString:[self buttonTitleAtIndex:self.cancelButtonIndex]]){
cancelButton = button;
} else if ([button.titleLabel.text isEqualToString:tempButtonTitle]) {
tempButton = button;
}
}
}
if (hasCancelButton){ // move in cancel button
cancelButton.frame = tempButton.frame;
}
[tempButton removeFromSuperview];
// Find lowest button still visable.
CGRect lowestButtonFrame = CGRectZero;
for (UIButton *button in self.subviews) {
if ([button isKindOfClass:[UIButton class]]){
if (button.frame.origin.y > lowestButtonFrame.origin.y){
lowestButtonFrame = button.frame;
}
}
}
// determine new height of the alert view based on the lowest button frame
CGFloat newHeight = CGRectGetMaxY(lowestButtonFrame) + (lowestButtonFrame.origin.x * 1.5);
self.bounds = CGRectMake(0, 0, self.bounds.size.width, newHeight);
}
}
The way this method accomplishes it's goal is to add a temporary button to the alert-view to force the alert-view to stack the buttons, then it removes the temporary button and adjusts the height. Since it's a category method you use it simply by calling:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Test title" message:#"message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[alert showWithButtonsStacked];
This code results in an alert like this:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:nil] autorelease];
[alert addButtonWithTitle:button1Title];
[alert addButtonWithTitle:#"Cancel"];
[alert addButtonWithTitle:button2Title];
[alert setCancelButtonIndex:1]; // to make it look like cancel button
[alert show];
Set the cancel button to nil and just add it in the other buttons instead
I've created a Master-Detail Application with Xcode 4.3.
In the Master View I want to show the user an alert when the Add (+) button is pressed?
In which method should I place the code for the alert?
Any help will be apprecciated.
The method is already created and bound by the template so just change the insertNewObject method like this
- (void)insertNewObject:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"This is an alert" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}
Use following code,
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addButtonActionName)];
It will display + button in right top corner of navigation bar.
-(void) addButtonActionName {
// Your code for the Alert view
}
you should creat an
-(IBAction)SomeAction{
//display your alert view
}
dont forget to hook it on IB
Below is the code for adding BarButton in Navigation Bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(ShowMyAlert:)];
& this is for the event handling...
- (void)ShowMyAlert:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Test Alert Message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
I have UIAlertView which is being displayed upon the load of a view.
av = [[UIAlertView alloc]initWithTitle:#"New Password" message:#"please enter a new passward" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"done", nil];
av.alertViewStyle = UIAlertViewStyleSecureTextInput;
[av becomeFirstResponder];
[av show];
However the keyboard is not being display on the iPad or the simulator?
I have also tried
I just tried
[av becomeFirstResponder];
and also
UITextField *text = [alertView textFieldAtIndex:0];
[text becomeFirstResponder];
I just tried this piece of code and it logs that the textField is the first responder but still no keyboard.
if([[av textFieldAtIndex:0] isFirstResponder] == YES){
NSLog(#"av is the first responder.");
}
Making the alert into the first responder won't help. You need to make te text box inside the alert view into the first responder.
Edit:
You may need to call reloadInputViews (with or without the s, don't remember). Also double check that you're not changing the input views anywhere that might be breaking them.
Edit 2:
You might want to move the alert from viewDidLoad into viewDidAppear. I've seen problems with UI elements being updated/presented too early. This is one of those cases, I think.
To use
av = [[UIAlertView alloc]initWithTitle:#"New Password" message:#"please enter a new passward" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"done", nil];
av.tag=1;
av.alertViewStyle = UIAlertViewStyleSecureTextInput;
[av show];
and use this method.
- (void)alertView:(UIAlertView *)alertViews clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertViews.tag==1)
{
[textfieldname becomeFirstResponder];
}
}
this works
self.alertView = [[UIAlertView alloc]initWithTitle:#"Login" message:#"Please enter you mobile number" delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil];
self.alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
[alertView show];
but not this
UIAlertView* alertView = [[UIAlertView alloc] init];
[alertView initWithTitle:...]
av = [[UIAlertView alloc]initWithTitle:#"New Password" message:#"please enter a new passward" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"done", nil];
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
CGAffineTransform Transform = CGAffineTransformMakeTranslation(0, 60);
[tf setBackgroundColor:[UIColor whiteColor]];
[av setTransform:Transform];
[av addSubview:tf];
[av show];
This works but you should be able to do this in IOS 5 using UIAlertViewStyleSecureTextInput??
I had the same issue, but my solution looks like it might not apply to original poster...
I had this code :
UIAlertView* alert = [[UIAlertView alloc] init];
[alert initWithTitle:...]
When run, no keyboard appeared.
I changed it to this, and keyboard now appears:
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:...];
Doing the init after alert was created seem to leave internal state of object as "this object doesn't need a keyboard!"
I have been trying this one tonight for a while.... I have a UIActionSheet in its own controller. I import the controller in the view I would like it to be displayed. I can't figure out how to make a new view be shown when the user presses one of the buttons (you can see where I just send an alert right now). Any ideas?
- (void)helpButtonPressedView {
// open a dialog with two custom buttons
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#""
delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil
otherButtonTitles:#"Help", #"Credits", nil];
[self parentViewController];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view]; // show from our table view (pops up in the middle of the table)
[actionSheet release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(#"Button Pressed: %d",buttonIndex);
int btn = buttonIndex;
UIAlertView *alert;
switch (btn) {
case 0:
// help screen
alert = [[UIAlertView alloc] initWithTitle:#"Button Press Notice" message:#"The Help screen will be dispayed"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
// helpViewController = [[HelpViewController alloc] init];
// [self presentModalViewController:helpViewController animated:YES];
break;
case 1:
// credits screen
alert = [[UIAlertView alloc] initWithTitle:#"Button Press Notice" message:#"The Credits screen will be dispayed"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
break;
default:
break;
}
}
Here is a pic of what I am trying to do... When the user presses on the "Help" button, a new view shows. When the user presses the "Credits" button a different view shows. I am using IB to create the views.
alt text http://thecoolestcompany.com/UIActionSheet-view.jpg
Instead of the alert, you could create your view and add it to the current view:
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(100,100,100,100)];
[self.view addSubview:newView];
[newView release];
It has been a while since I asked this question -- and I figured it out a while ago.... I was missing the navigation controller to do a push of a view controller. In other words, create the view controller then push the view using a navigation controller. If anyone is reading this and needs sample code, post against the question and I will post the sample code...