Code breakpoint - objective-c

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.

Related

UIAlertView does not work with UIBarButton

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

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

Updating UIAlertView Message dynamically and newline character issue

I need to display multiple lines of text in the message of my UIAlertView. I have tried adding a '\n', but it has no effect. It still displays: "This is an examp....".
HOWEVER, if I turn my iPhone to landscape mode it displays the message as I intend it to. And then if I switch BACK to portrait mode it displays correctly there as well.
Update: After further consideration, I suspect it has something to do with the fact that I am updating the current message with a new (and much longer) string. I have already called "show" on the alert view, and am trying to update the message. Perhaps something needs to be redrawn? Like i said before, it displays correctly if I change orientations (doesn't matter which orientation I start in, i still have the same problem). i have already tried "setNeedsDisplay" and "setNeedsLayout".
Although I believe updating the alert view's text while it's being displayed is wrong, the only way I see to change it is this way
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"test" message:#"this is a message\nthis is a new line text" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:#"test button",nil];
[alert show];
//set the new message
NSString *newMessage = [NSString stringWithString:#"Test\nWith a new message with\ncustom new line string\n and a lot\n of new lines\n Yep"];
[alert setMessage:newMessage];
//get the original alert Frame
CGRect alertFrame = alert.frame;
//get the alert view Label
UILabel *alertLabel = [alert.subviews objectAtIndex:2];
//and get the original frame position
CGRect alertLabelFrame = alertLabel.frame;
//see how much space we are needing for the new message and adjust
int heightChange = [newMessage sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(alertLabelFrame.size.width, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap].height - alertLabelFrame.size.height;
alertFrame.size.height += heightChange;
//adjust the Label height to the new height
alertLabelFrame.size.height += heightChange;
//adjust the frame and elements with an animation for smoothness
[UIView animateWithDuration:0.15 delay:0.4 options:(UIViewAnimationCurveEaseIn) animations:^{
[alert setFrame:alertFrame];
alertLabel.frame = alertLabelFrame;
//move any buttons
for (UIView *subView in alert.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
//this is a button move it
UIButton *button = (UIButton*)subView;
CGRect alertButtonFrame = button.frame;
//adjust button Y position to the new height
alertButtonFrame.origin.y += heightChange-5;
button.frame = alertButtonFrame;
}
}
} completion:nil];
For newline use the \n newline character in your text like this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"test" message:#"this is a message\nthis is a new line text" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
Even with setMessage this works:
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setMessage:#"this is\na test"];
[alert show];
[alert release];
Use '\r' symbol. It should break line properly.
Here is some simple code to resize the messages in an alert. Note that the alert's frame needs to be large enough. You can pad the initial alert with a few newlines.
for (UILabel *l in alertView.subviews){
if ([l isKindOfClass:[UILabel class]]){
float w = l.frame.size.width;
[l setNumberOfLines:0];
[l sizeToFit];
CGRect r = l.frame;
r.size.width = w;
l.frame = r;
}
}
Create your own AlertView Category.
AlertWithBlock.h file
#import <UIKit/UIKit.h>
#interface UIAlertView (AlertWithBlock)
- (void (^)(UIAlertView *alertView, NSInteger buttonIndex))delegateBlock;
- (void)setDelegateBlock:(void (^)(UIAlertView *alertView, NSInteger buttonIndex))delegateBlock;
+ (void)alertViewWithTitle:(NSString *)title buttonTitle:(NSString *)buttonTitle message:(NSString *)message;
+ (UIAlertView *)alertWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles delegate:(void (^)(UIAlertView *alertView, NSInteger buttonIndex))delegate;
#end

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.