UIAlertView does not work with UIBarButton - objective-c

I have a strange problem:
Trying to call an action which contains NSLog and UIAlert using a UIBarButtonItem from a NavigationController. NSLog prints and UIAlert doesn't appear…
-(void)addNewItem
{
UIAlertView *alertAddnewItem = [[UIAlertView alloc]initWithTitle:#"Hello!" message:#"hello!" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
[self.view addSubview:alertAddnewItem];
NSLog(#"Hello!");
}
I guess the problems is with [self.view addSubview:alertAddnewItem];
Thank you!

call:
[alertAddnewItem show];
not
[self.view addSubview:alertAddnewItem];

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];

Code breakpoint

Hi I have a code that when I run quits and says there is a breakpoint
- (void)checkCollision{
if(CGRectIntersectsRect(penny.frame, hand.frame))
{
[randomMain invalidate];
[startButton setHidden:NO];
pos= CGPointMake(0.0, 0.0);
CGRect frame = [penny frame];
frame.origin.x=137.0f;
frame.origin.y=326.0;
[penny setFrame:frame]; (the breakpoint is here)
CGRect frame2 = [hand frame];
frame2.origin.x=137.0f;
frame2.origin.y=20.0;
[hand setFrame:frame2];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"You Lose" message:[NSString stringWithFormat:#"He got the penny!"] delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
any ideas?
i apologize for the sloppy format Im new to the website, thanks!
Sometimes a breakpoint gets out of sync with the code presented. Try going to your breakpoint window (Command + 6 or View->Navigators->Show Breakpoint Navigator.
In that navigator, you may see active breakpoints. If you can find the errant breakpoint, delete it from the list. Otherwise, your best bet is to delete all your breakpoints.

Is it possible to create an UIAlertView without any buttons?

I need help with UIAlertView :-). Currently I have an UIAlertView showing up when the user shakes the device using the -(void)motionEnded: function. I want to make the alert view disappear after 0.5 seconds using a NSTimer so I don't need any button at the bottom of the alert view to dismiss it. Is there a way to create an UIAlertView without any button? [A way to remove the space with the arrow in the image below?]
Here's the code:
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
resetAlert = [[UIAlertView alloc] initWithTitle:#"Reset!"
message:nil
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil, nil];
[resetAlert show];
alertHideTimer = [NSTimer scheduledTimerWithTimeInterval:10.5 target:self selector:#selector(dismissWithClickedButtonIndex:animated:) userInfo:nil repeats:NO];
self.label.text = #"0";
numero = 0;
[self.label2 setHidden:YES];
}
Yes it is. Try the following: iToast
You can create your own view. Show and hide/close it as per your requirement, timers etc.
I'm not sure if having the NSTimer with a multiple argument selector is correct. I add this method:
-(void)dismissAlertView:(UIAlertView*)alertView {
[alertView dismissWithClickedButtonIndex:0 animated:NO];
}
and replace your NSTimer with:
[self performSelector:#selector(dismissAlertView:) withObject:resetAlert afterDelay:0.5];
As far as I know removing the space entirely is not possible.
However... here are some things you may want to try;
Adding top padding
Add a few newlines to get top padding: initWithTitle:#"\n\nConfiguring Preferences...
(source: iosdevelopertips.com)
Adding a spinner
Add a UIActivityIndicatorView to the UIAlertView
alert = [[UIAlertView alloc] initWithTitle:#"Configuring Preferences\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
Adjust the indicator so it is up a few pixels from the bottom of the alert
indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
[indicator startAnimating];
[alert addSubview:indicator];
(source: iosdevelopertips.com)
Hope that helps.
You can hide the alert simply by calling the delegate method after 5 sec
[alert dismissWithClickedButtonIndex:0 animated:YES];

Dismiss actionSheet before action under it's button is completed

My problem is that i have an ActionSheet, which is disappearing from screen only when action under this button is completed. My problem is that i want to click 'save' inside my action sheet, then dismiss action sheet and then to show some alert, informing user to wait until saving is done. Currently it works different: firstly action sheet is shown, then there is saving message UNDER action sheet, finally action sheet is removed from view.. so user doesnt see any alert message.
How to dismiss actionSheet earlier than xcode does it?
Method under sheetActionButton:
- (IBAction)saveAction:(id)sender
{
UIAlertView *alert;
alert = [[[UIAlertView alloc] initWithTitle:#"Saving photo to library\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease];
[alert show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
[indicator startAnimating];
[alert addSubview:indicator];
[indicator release];
[self saveImageToCameraRoll];
[alert dismissWithClickedButtonIndex:0 animated:YES];
}
You should move your saveImageToCameraRoll method onto a separate thread, or at least asynchronously on the main thread. Then you can dismiss the alert and saveAction: can return before it completes.
The simplest way to do this would be using dispatch_async. Use dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) to get a queue for a separate thread, or dispatch_get_main_queue() for the main thread. Make sure not to do any UI work (or use any APIs which aren't thread-safe) on other threads!
Edit: more detail:
- (IBAction)saveAction:(id)sender {
UIAlertView *alert;
alert = [[[UIAlertView alloc] initWithTitle:#"Saving photo to library\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease];
[alert show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
[indicator startAnimating];
[alert addSubview:indicator];
[indicator release];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Save in the background
[self saveImageToCameraRoll];
dispatch_async(dispatch_get_main_queue(), ^{
// Perform UI functions on the main thread!
[alert dismissWithClickedButtonIndex:0 animated:YES];
});
});
}

UIAlertView causes crash in Release mode

If the Help option is activated in my app when the user goes to the camera I show a UIAlertView first with tips on how to take a picture:
-(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [actionSheet cancelButtonIndex]) {
NSString *selectedButtonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([selectedButtonTitle isEqualToString:#"Camera"]) {
// If Help is activated display camera tips
if (helpEnabled == YES) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Camera Tips" message:#"\n\n\n\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Go To Camera"];
UIImageView *cameraHelpView = [[UIImageView alloc] initWithFrame:CGRectMake(17, 40, 250, 255)];
UIImage *cameraTutorial = [UIImage imageNamed:#"Camera_Tips.png"];
cameraHelpView.image = cameraTutorial;
[alert addSubview:cameraHelpView];
[cameraHelpView release];
[alert show];
[alert release];
}
}
}
This works in Debug mode but causes an "EXC BAD ACCESS" error in Release mode. I can present a new view controller modally from this point just fine, however the UIAlertView will always crash the app. Why?
I don't know why it works in debug mode, but it looks like you are releasing your cameraHelpView while it is still being used. In the subviews of alert is a pointer to cameraHelpView; when you release that, it can no longer be accessed. I would suggest replacing all of your -[NSObject release] calls in this context with -[NSObject autorelease]. Thus:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Camera Tips" message:#"\n\n\n\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Go To Camera"] autorelease];
UIImage *cameraTutorial = [UIImage imageNamed:#"Camera_Tips.png"];
UIImageView *cameraHelpView = [[[UIImageView alloc] initWithFrame:CGRectMake(17, 40, 250, 255)] autorelease];
cameraHelpView.image = cameraTutorial;
[alert addSubview:cameraHelpView];
[alert show];
Go ahead and try that and let me know if it works. Good luck!
I found my mistake. I wasn't passing nil as the final argument to otherButtonTitles! Debug mode must see and fix this error for you. Hope this helps someone.