2014-04-08 80 views
0

NSMutableArray按距离排序。所以我需要一个呼叫按钮或标签UINavigationBar(右键)与调用函数使用字典和valueKey。我怎么能意识到它?导航栏中的呼叫按钮

下面是代码,我使用的是:

- (void)updateViewWithDict:(NSDictionary*)dic { 
    NSString *str = [NSString stringWithFormat:@"%@\nAddress: %@\nPhone: %@\n\nWorkTime:\n%@", [dic valueForKey:@"name"], [dic valueForKey:@"address"], [dic valueForKey:@"phone"], [dic valueForKey:@"workTime"]]; 

    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 110, 300, 140)]; 
    nameLabel.textAlignment = NSTextAlignmentLeft; 
    nameLabel.numberOfLines = 0; 
    nameLabel.text = str; 
    nameLabel.textColor = [UIColor whiteColor]; 
    nameLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:14.0]; 

    UIBarButtonItem *callButton = [[UIBarButtonItem alloc] initWithTitle:@"Call" 
        style:UIBarButtonItemStyleDone target:nil action:nil]; 

    self.navigationItem.rightBarButtonItem = callButton; 
} 

我不能得到如何分配NSURL我的按钮?

我试图使用这样的代码..但接下来呢?

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",[dic valueForKey:@"phone"]]]]; 

===

解决方案: - 我发现像这样的方式:

- (void)updateViewWithDict:(NSDictionary*)dic

我写if语句

if ([[dic valueForKey:@"phone"] isEqual: @"12345"]){ 

     UIBarButtonItem *callButton = [[UIBarButtonItem alloc] initWithTitle:@"Call" style:UIBarButtonItemStyleDone target:self action:@selector(FirstOffice:)]; 

     self.navigationItem.rightBarButtonItem = callButton; 

    } 

    else if ([[dic valueForKey:@"phone"] isEqual: @"67890]){ 

     UIBarButtonItem *callButton2 = [[UIBarButtonItem alloc] initWithTitle:@"Call" style:UIBarButtonItemStyleDone target:self action:@selector(SecondOffice:)]; 

     self.navigationItem.rightBarButtonItem = callButton2; 
    } 

和方法,这样

- (void)FirstOffice:(UIBarButtonItem*)sender { 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://12345"]]]; 
} 

- (void)SecondOffice:(UIBarButtonItem*)sender { 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://67890"]]]; 
} 

也许还有另一种方式?更简单的方法)谢谢

回答

0

initWithTitle:style:target:action:targetaction参数用于上单击操作:

UIBarButtonItem *callButton = [[UIBarButtonItem alloc] initWithTitle:@"Call" style:UIBarButtonItemStyleDone target:self action:@selector(buttonTapped)]; 

然后定义方法:

-(void)buttonTapped 
{ 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",[dic valueForKey:@"phone"]]]]; 
} 
0

将此

UIBarButtonItem *callButton = [[UIBarButtonItem alloc] initWithTitle:@"Call" 
       style:UIBarButtonItemStyleDone target:self action:@selector(callSomeone:); 

并添加方法

- (void)callSomeone:(UIBarButtonItem*)sender { 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",[dic objectForKey:@"phone"]]]]; 
} 
+0

我想这样的事情,但在虚空方法[DIC valueForKey ...]不认识... – diegogabisonia

+0

'-objectForKey:'使用词典交互时英寸 – Hyperbole

+0

编辑我的答案。 – dasdom