I have an actionSheet with EDIT and DELETE buttons, both being other buttons This is the Code i have written for it
-(void)method1
{
action = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:#"Edit", #"Delete", nil];
action.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[action showInView:self.view];
[action release];
}
I have used the deleate method to assign actions to method..
-(void)actionSheet:(UIActionSheet *)action didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
{
// do something
}
if(buttonIndex == 1)
{
// do something
}
}
now the problem is that the actionsheet does not dismiss at one click of either of the buttons.. Please help me with some solution.
This appears to be a bug in iOS4.0. I had this issue in my simulator. I changed the version to 4.3 and 5.0 and it seemed ok.
Edit:
Seems that my issue was more specifically to do with the actionsheet being launched twice by a delegate method "-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField"
Not sure why this is called twice in this case but not others (again I assume a iOS 4.0 bug that's been fixed in later releases). My workaround is to keep track if it's been called already and not call it a second time.
Edit 2
I would suggest doing something like:
-(void)method1
{
if(hasLaunchedActionSheet)
{
return;
}
hasLaunchedActionSheet = YES;
...
and:
-(void)actionSheet:(UIActionSheet *)action didDismissWithButtonIndex:(NSInteger)buttonIndex
{
hasLaunchedActionSheet = NO;
...
For me, the issue's not so much in Xcode as it is in the iOS SDK itself calling my event twice. I'm not sure how you're calling method1 so it might be a different issue with a different event.
You are using wrong delegate methods, for button interaction, you should use:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
instead of:
-(void)actionSheet:(UIActionSheet *)action didDismissWithButtonIndex:(NSInteger)buttonIndex
Related
Let's say John has been using my app for 3-6 minutes.
Then I'd like a view to pop up that would in my case, include an advertisement.
Something like this,
AdViewController *adViewController = [[AdViewController alloc] init];
[self presentViewController:adViewController animated:YES completion:nil];
But how can I let it pop up after some random time? I guess I have to work with the delegate files and use the arc4random function.
After John has viewed the advertisement, he'd then have to close it, but that's not the problem..
Can someone give me a code example?
The simple solution is to
Create a NSTimer and let it fire every 300 secs (5 mins)
NSTimer will fire an action that shows your pop-up.
I don't get it why was this so difficult to understand?
//use arc4random() if you need random time
NSTimer *timer2 = [NSTimer scheduledTimerWithTimeInterval:300.0 target:self selector:#selector(rateThisApp) userInfo:nil repeats:YES];
// *********
// ********* RATE APP ***********
// *********
- (IBAction)rateThisApp
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Rate this App"
message:#"Are you enjoying this app? Please leave a rating at the app store and tell us what you think of this app and its features. We would love to hear from you!"
delegate:self cancelButtonTitle:#"Not Now"
otherButtonTitles:#"Rate Now", nil];
[alert show];
alert.tag = 400;
}
-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (actionSheet.tag == 400)
{
if (buttonIndex == 0)
{
//dont do anything, user hit cancel
}
else if (buttonIndex == 1)
{
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:#"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=1234567"]];
}
}
}
Below is the action and alert view. Why Won't This Work When The User Taps The Button?
Alert/Action
-(IBAction)myButton
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"" message:#"Call (804) 378-7120?"
delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"YES",nil];
[alert show];
[alert release];
}
What To Do With User Input
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==1)
{
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:#"tel://(804) 378-7120"]];
}
else
{
//Do whatever you want
}
}
As far as I remember, the index of the 1st other button is 0. So you say
if(buttonIndex==0)
{...open you URL...}
else
{
NSLog(#\"cancel\");
}
Per Apple documentation for Phone Links, the format is:
tel:1-408-555-5555
Similar to an email link, there is no //, and notice the use of hyphens, not parenthesis or space.
use this method when the user hit the YES option.
**-(void)phoneCall
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:099123456"]];
}**
Change the number 099123456 to the number what you want to call.
Hope this result useful.
I am using UIImagePickerController to capture video from my app and i have set video maximum duration to 30 seconds. When that 30 seconds limit is reached. I get an alert with a message "maximum video recording limit reached" produced by UIImagePickerController and it stops capturing video.
What I want is that I want to respond to that alert that is generated automatically when 30 seconds limit is reached. I want to perform some action when "OK" button of that alert is pressed. I have implemented all the delegate methods of UIAlertView but it does come in any method when I press OK button.
Please help me how I can respond to that alert?
You can't use all those delegate methods because you didn't initiate the UIAlertView so you can't set his delegate...
The only thing I can think about is to do somethong like listening to the UIWindowDidBecomeVisibleNotification to detect when an alert is shown and to the UIWindowDidBecomeHiddenNotification notification to detect when it disappears.
You should know that those notification will fire for all kind of components that uses their own UIWindow such as UIActionSheet or the keyboard, so you need to make sure this is the right one (maybe check to see if there is a UIAlertView in one of the subviews..)
Set yourself as a delegate of your UIImagePickerController, and implement the UIImagePickerControllerDelegate protocol. Specifically, the following method:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)
Use UIAlertViewDelegateProtocol
Form docs
alertView:clickedButtonAtIndex:
Sent to the delegate when the user
clicks a button on an alert view.
(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex Parameters alertView The
alert view containing the button. buttonIndex The index of the button
that was clicked. The button indices start at 0. Discussion The
receiver is automatically dismissed after this method is invoked.
Availability Available in iOS 2.0 and later. Declared In UIAlertView.h
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
{
// do stuff for button index 0,ie cancel button and sthe same for other button indeces
}
}
Please refer this tutorials : http://mobile.tutsplus.com/tutorials/iphone/uialertview/ you can get more ideal about this :
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Hello World!"
message:#"This is your first UIAlertview message."
delegate:self
cancelButtonTitle:#"Button 1"
otherButtonTitles:#"Button 2", #"Button 3", nil];
[message show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Button 1"])
{
NSLog(#"Button 1 was selected.");
}
else if([title isEqualToString:#"Button 2"])
{
NSLog(#"Button 2 was selected.");
}
else if([title isEqualToString:#"Button 3"])
{
NSLog(#"Button 3 was selected.");
}
}
I have tested this on both an iPad and the iPad Simulator and after one successful shake, the motionBegan method is never called. The following is a snippet of my program.
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if(event.type == UIEventSubtypeMotionShake)
{
NSLog(#"Shake event occurred.");
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Information" message:#"Some message here" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
alert.tag = shakeTag;
[alert show];
}
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:NO];
[self becomeFirstResponder];
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:NO];
}
-(void)viewDidDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewDidDisappear:NO];
}
What exactly is preventing the motionBegan method from occurring again? I would like the UIAlertView to be presented exactly once for the first shake and dismissed for all subsequent shakes. I have a hunch that the First Responder is still attached to the UIAlertView which is preventing the motionBegan method from being called again.
Update: In my corresponding UIView, there is an UIActionSheet that is created. which is called and implemented in my UIView) and I trigger the motionBegan method (which is in my UIViewController) at the same time which the UIActionSheet is displayed on the screen, the problem where the motionBegan method no longer being able to be called exists.
Afterwards, the UIAlertView is dismissed from any button selection, the motionBegan method no longer is called but the UIActionSheet works perfectly fine. There is no firstResponder assignment in UIView and only the "canBecomeFirstResponder" exists in the UIViewController. Any ideas?
Since that code is in your viewController, remove all the stuff related to responder chain. You don't need it actually. It is made automatically for you. More precisely you can remove:
- (BOOL)canBecomeFirstResponder // remove all that method
{
return YES;
}
[self becomeFirstResponder]; // remove this line
...
[self resignFirstResponder]; // remove this line
...
and remove this as well all that method
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
Call your alertView stuff in motionEnd instead motionBegan. It could be better.
In my app, if location services is turned off, it prompts the user: Turn On Location Services to Allow "MyAPP" to Determine Your Location.
The two option buttons are Settings and Cancel.
How do I handle when the user presses cancel?
Is there any delgate method to handle the cancel button press ?
Maybe this might work for what you want.
First, conform to the UIAlertViewDelegate protocol in your header file.
Then there's a delegate method you can implement.
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// if the alert view that appeared has titled "Location Denied" and user pressed on cancel button (cancel button is button at index 0)
if(alertView.title isEqualToString:#"Location Denied"] && buttonIndex == 0)
{
// do something
}
}
// through Button name in UIAlertview you can access which Button you are pressed*
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"cancel"])
{
}
}