This question already has answers here:
Pass a NSDictionary as parameter to UITapGestureRecognizer
(3 answers)
Closed 7 years ago.
I am newbie too ios and objective c,I know my question is very simple but i found not solution do asked,I am having a method for a static value,now i want to make it dynamic by passing value to that method,So can anybuddy please tell me how to write method with parameter and how to call it.my code is as below:
-(void)phoneNumberLabelTap
{
NSURL *phoneUrl = [NSURL URLWithString:[NSString stringWithFormat:#"telprompt:%#",fonlabel.text]];
if ([[UIApplication sharedApplication] canOpenURL:phoneUrl]) {
[[UIApplication sharedApplication] openURL:phoneUrl];
} else {
UIAlertView * calert = [[UIAlertView alloc]initWithTitle:#"Alert" message:#"Call facility is not available!!!" delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[calert show];
}
}
I want to add a parameter to this method,
And i am calling this method as below,
fonlabel.userInteractionEnabled = YES;
homeLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(phoneNumberLabelTap)];
[fonlabel addGestureRecognizer:tapGesture];
[homeLabel addGestureRecognizer:tapGesture];
- (void)setPhoneNumber:(NSString *)phoneNumber; // set a phoneNumber property
- (void)prepareToBeTapped
{
UITapGestureRecognizer *tapFirstGuy = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(makeCallToThisPerson:)];
[self addGestureRecognizer:tapFirstGuy];
}
- (void)makeCallToThisPerson:(id)sender
{
NSString *phoneURL = [NSString stringWithFormat:#"tel:%#", phoneNumber];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneURL]];
}
Instead of giving a UILabel, you can give a UIButton and can give the same method as target for the button
UIButton *fonButton = [[UIButton alloc] init];
[fonButton addTarget:self action:#selector(phoneNumberButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
and in the target method like this
-(void)phoneNumberButtonClicked:(UIButton *)sender
{
NSURL *phoneUrl = [NSURL URLWithString:[NSString stringWithFormat:#"telprompt:%#",sender.titleLabel.text]];
if ([[UIApplication sharedApplication] canOpenURL:phoneUrl]) {
[[UIApplication sharedApplication] openURL:phoneUrl];
} else {
UIAlertView * calert = [[UIAlertView alloc]initWithTitle:#"Alert" message:#"Call facility is not available!!!" delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[calert show];
}
}
One gesture can be applied to one object. You need to create 2 gestures for both labels. Below code with small changes will resolve your problem.
-(void)phoneNumberLabelTap:(UITapGestureRecognizer*)tapRecognizer
{
UILabel* lblPhone = (UILabel*)tapRecognizer.view;
NSURL *phoneUrl = [NSURL URLWithString:[NSString stringWithFormat:#"telprompt:%#",lblPhone.text]];
if ([[UIApplication sharedApplication] canOpenURL:phoneUrl]) {
[[UIApplication sharedApplication] openURL:phoneUrl];
} else {
UIAlertView * calert = [[UIAlertView alloc]initWithTitle:#"Alert" message:#"Call facility is not available!!!" delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[calert show];
}
}
fonlabel.userInteractionEnabled = YES;
homeLabel.userInteractionEnabled = YES;
[homeLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(phoneNumberLabelTap:)]];
[fonlabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(phoneNumberLabelTap:)]];
Related
Im trying to put in a rate App alert, but its not opening properly, in fact when I click the Yes button it dosnt open the url at all, How do I make it work?
Below is the call in the viewdidload:
The below code in viewdidload is also in app delegate
[super viewDidLoad];
//rate app
NSUserDefaults *prefsr = [NSUserDefaults standardUserDefaults];
NSInteger launchCount = [prefsr integerForKey:#"launchCount"];
if (launchCount >= 1) {
UIAlertView *alertRate = [[UIAlertView alloc] initWithTitle:#"Like this app?" message:#"Rate on the app store." delegate:nil cancelButtonTitle:#"No, thanks" otherButtonTitles:#"Yes",#"Remind me later", nil];
[alertRate show];
}
-(void)alertViewRate:(UIAlertView *)alertViewRate clickedButtonAtIndex:(NSInteger)buttonIndexP{
if (buttonIndexP == 2) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.google.com"]];
}
else if (buttonIndexP == 1){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.google.com"]];
}
Add the delegate Self while creating the UIAlertView
UIAlertView *alertRate = [[UIAlertView alloc] initWithTitle:#"Like this app?" message:#"Rate on the app store." delegate:self cancelButtonTitle:#"No, thanks" otherButtonTitles:#"Yes",#"Remind me later", nil];
[alertRate show];
Delegate method for UIAlertView also need to be chnaged
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 2) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.google.com"]];
}
else if (buttonIndex == 1){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.google.com"]];
}
}
You haven't set the delegate to self while you are creating UIAlertView object. So -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex method is not getting called when you click on buttons in the AlertView.
Change your code From
UIAlertView *alertRate = [[UIAlertView alloc] initWithTitle:#"Like this app?" message:#"Rate on the app store." delegate:nil cancelButtonTitle:#"No, thanks" otherButtonTitles:#"Yes",#"Remind me later", nil];
To
UIAlertView *alertRate = [[UIAlertView alloc] initWithTitle:#"Like this app?" message:#"Rate on the app store." delegate:self. cancelButtonTitle:#"No, thanks" otherButtonTitles:#"Yes",#"Remind me later", nil];
I need to add a button Facebook, Then the action should take user to Facebook, I'm just not sure how to do it from the code below?
-(IBAction)btContinueClick:(id)sender{
if(Level == [list count]){
UIAlertView *info = [[UIAlertView alloc]initWithTitle:#"Good" message:#"Go to facebook?" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"Facebook"];
[info show]; //// Change Game End Message
}
}
If you want to go to the native app of Facebook in iOS.
Using delegate method of alertView
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1) {
NSURL *url = [NSURL URLWithString:#"fb://profile"];
if ([[UIApplication sharedApplication] canOpenURL:url]) { //USE FB NATIVE APP
[[UIApplication sharedApplication] openURL:url];
}
else { //IF THE DEVICE IS UNABLE TO OPEN IN NATIVE APP, USE BROWSER INSTEAD
NSURL *browserURL = [NSURL URLWithString:#"http://www.facebook.com"];
[[UIApplication sharedApplication] openURL:browserURL];
}
}
}
Make sure you have added UIAlertViewDelegate in .h file
-(IBAction)btContinueClick:(id)sender{
if(Level == [list count]){
UIAlertView *info = [[UIAlertView alloc]initWithTitle:#"Good" message:#"Go to facebook?" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"Facebook"];
[info show]; //// Change Game End Message
info.delegate=self;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if(buttonIndex==1)
{
NSLog(#"Facebook selected.");
NSURL *url = [NSURL URLWithString:#"fb://profile/<id>"]; // provide the app ID
[[UIApplication sharedApplication] openURL:url];
}
}
The UIAlertView Delegate is not getting called. The alert is showing but the buttons do not work. I want one button to open google maps and the other button to open native maps app.
-(IBAction)showAlertView {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Obrir en..."
message:#""
delegate:self
cancelButtonTitle:#"Millor no..."
otherButtonTitles:#"Mapes",#"Google Maps",nil];
[alert show];
}
-(void)showAlertView:(UIAlertView *)alert clickedButtonAtIndex (NSInteger)otherButtonTitles
{
NSString *title = [alert buttonTitleAtIndex:otherButtonTitles];
if([title isEqualToString:#"Mapes"])
{
UIApplication *ourApplication = [UIApplication sharedApplication];
NSString *ourPath = #"http://maps.apple.com/?q=41.11670,1.25812";
NSURL *ourURL = [NSURL URLWithString:ourPath];
[ourApplication openURL:ourURL];
}
if([title isEqualToString:#"Google Maps"])
{
UIApplication *ourApplication = [UIApplication sharedApplication];
NSString *ourPath = #"http://maps.apple.com/?q=41.11670,1.25812";
NSURL *ourURL = [NSURL URLWithString:ourPath];
[ourApplication openURL:ourURL];
}
}
#end
Switch :
-(void)showAlertView:(UIAlertView *)alert clickedButtonAtIndex (NSInteger)otherButtonTitles
to
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
A button from the first alert executes the second alert which is basically a confirmation to call someone. I don't get any errors but it doesn't work.
When I press on the second alert the Call button it crashes
-(void) alertView: (UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonString = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonString isEqualToString:#"Phone"])
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Notice!" message:#"You are about to call .... Do you wish to continue?" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"Call", nil];
[alert2 show];
if ([buttonString isEqualToString:#"Call"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"tel://12345678"]]];
}
if([buttonString isEqualToString:#"Website"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"website"]];
}
if ([buttonString isEqualToString:#"Facebook"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.facebook.com/groups/..../"]];
}
}
}
You assigned to delegate as a nil in second alertview. That's why the alertView:clickedButtonAtIndex: delegate method is not called in second time. So you should assign to delegate as a self.
-(void) alertView: (UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonString = [alertView buttonTitleAtIndex:buttonIndex];
if([buttonString isEqualToString:#"Phone"]) {
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Notice!" message:#"You are about to call .... Do you wish to continue?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Call", nil];
[alert2 show];
}
if([buttonString isEqualToString:#"Call"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"tel://12345678"]]];
}
if([buttonString isEqualToString:#"Website"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"website"]];
}
if([buttonString isEqualToString:#"Facebook"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.facebook.com/groups/..../"]];
}
}
I think it will be helpful to you.
Launch the second alert with a delay. Problem is - alert needs some time to hide itself, before any new alerts can appear, thus directly calling second one will not work.
try this for example:
- (void) alertView: (UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonString = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonString isEqualToString:#"Phone"])
{
[self performSelector:#selector(callSecondAlertView) withObject:nil afterDelay:1];
}
}
- (void)callSecondAlertView
{
//show new alert view
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Notice!" message:#"You are about to call .... Do you wish to continue?" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"Call", nil];
[alert2 show];
[alert2 release];
}
If not working, or too long delay - play with afterDelay: value.
got it! for some reason the [[[alertView subviews] objectAtIndex:6] setBackgroundColor... for the fisrt alertView buttons i had interferes with the second alertView functionality. xcode though did not direct me directly to the problem until i updated xcode to 4.4.1
Use the alertView:didDismissWithButtonIndex: delegate method instead of the alertView:clickedButtonAtIndex: delegate method. The former is called after the alert is gone. This make more sense when you want to show a second based on the tapped button of the first alert view.
Also give tag to UIAlertview objects.
I have this normal code for notification alert:
- (void)saveNotification {
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil) {
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [[datePicker date] dateByAddingTimeInterval:-20];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = #"Evaluation Planner";
notif.alertAction = #"Details";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:reminderText.text
forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
}
- (void)showReminder:(NSString *)text {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Reminder"
message:text delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
This works perfectly well for me but when I click onto "Details", I want it to be redirected to a particular page. Instead this only redirect to the main page of the application and I have to click through the whole app all the way to the reminder section to view it. I'm not exactly sure how to fix it. Do help me.
Thanks.
Your app delegate method application:didFinishLaunchingWithOptions: will get called with a notification payload. It's all documented well in Local and Push Notification Programming Guide.