2014-04-08 79 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