2013-04-30 73 views
0

我正在開發一個iPad應用程序。在某個階段,我需要使用下拉式功能。所以,我使用UIPopoverView相同。從其他視圖更新UIButton標題...?

當IBAction觸發特定的UIButton時,我調整popoverview呈現UITableViewController。

而且一切工作正常。我需要當用戶點擊任何一個單元格時,相關的單元格值需要在附加的UIButton標題中設置。

enter image description here

這裏,酥料餅的視圖是的UITableViewController視圖,它予單獨創建。並在選擇Outlet IBAction時調用它。

CGRect dropdownPosition = CGRectMake(self.btnOutlet.frame.origin.x, self.btnOutlet.frame.origin.y, self.btnOutlet.frame.size.width, self.btnOutlet.frame.size.height); 
[pcDropdown presentPopoverFromRect:dropdownPosition inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; 

由於

+0

從表格視圖更新您的按鈕文本做選擇方法 – 08442 2013-04-30 12:06:30

+0

感謝您的回覆,是的,我想要的東西。但我不知道如何實現這一點。請幫我一些代碼。 – 2013-04-30 12:08:27

+0

從表視圖調用委託方法確實選擇了您的主視圖控制器,傳遞所需的信息,然後更新委託方法內的按鈕。 – 2013-04-30 12:20:03

回答

4

Sangony答案几乎是正確的,但有一些細微的變化,而不是註冊方法不帶參數作爲觀察員,你應該增加它承認一個參數:

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(someAction:) 
              name:@"ButtonNeedsUpdate" 
              object:nil]; 

然後,當你發佈notifica重刑(在你的表的觀點didSelectRow:atIndexPath :),你可以添加一個對象(NSDictionay)作爲用戶信息:

//... 
NSDictionary *userInfoDictionary = @{@"newText":@"some text"}; 
[[NSNotificationCenter defaultCenter] postNotificationName:@"ButtonNeedsUpdate" 
                object:self 
                userInfo:userInfoDictionary]; 
//... 

然後在被觀察​​此通知的類,可以用數據進行工作在someAction動作方法是這樣的:

-(void)someAction:(NSNotification)notification{ 
    NSString *textForTheButton = [[notification userInfo]objectForKey:@"newText"]; 
    [self.myButton setTitle:textForTheButton 
        forState:UIControlStateNormal]; 
    //... 
} 

你的按鈕標題現在應該是「一些文本」。

+0

優雅而簡單,比設置單身。 – sangony 2013-04-30 16:14:36

+1

需要刪除自己作爲觀察員或將崩潰。 – Mundi 2013-05-01 15:36:51

1

實現與像didSelectItemWithTitle:的方法的代表協議。使視圖控制器將按鈕控制在彈出窗口中的視圖控制器的委託。當選中一行時,通知可以更新該按鈕的委託人。

// MainController.h 
#include "PopupTableController.h" 
@interface MainController : UIViewController <PopUpListDelegate> 

// PopupTableController.h 
@protocol PopUpListDelegate; 
@interface PopupTableController : UITableViewController 
... 
@property (nonatomic, assign) id <PopUpListDelegate> delegate; 
... 
@end 

@protocol PopUpListDelegate 
-(void)didSelectItem:(NSUInteger)item; 
@end 

// PopupTableController.m 
// in didSelectRowAtIndexPath: 
if (self.delegate) { 
    [self.delegate didSelectItem:indexPath.row]; 
} 

// MainController.m 
// where you push the table view or prepareForSegue 
popupTableController.delegate = self; 

// and 
-(void)didSelectItem:(NSInteger)item { 
    // update the button based on item 
} 
+0

我嘗試你的解決方案,但有一些錯誤** self.delegate didSelectItem:indexPath.row]; **告訴沒有已知的實例方法選擇器'didSelectItem'在** PopupTableController.m **以及**不完整的實現* *。 – 2013-04-30 13:00:08

+0

您必須設置委託並實施該方法。這一切都在上面指出。 – Mundi 2013-04-30 13:03:05

+0

謝謝,它有幫助,但有了這個我可以管理一個UIButton標題,實際上有多個UIButton可以調用相同的UITableViewController ...!?! – 2013-04-30 13:31:18

2

嘗試使用NSNotificationCenter。在包含您的按鈕將該代碼放在VC:

[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(someAction) 
               name:@"ButtonNeedsUpdate" 
               object:nil]; 

-(void)someAction { 
// do stuff to your button 
} 

而且在任何其他VC會使按鈕進行修改,把這個代碼,以使一個通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"ButtonNeedsUpdate" object:self]; 
+0

感謝您的回覆,但是如何識別哪些文本必須設置,導致沒有** someAction的參數** – 2013-04-30 13:40:55

+0

您可以設置一個Singleton並使用它來存儲字符串的值(s)爲文本標籤。如果您願意,我可以發佈代碼。 – sangony 2013-04-30 13:55:27