2012-10-06 32 views
3

執行那叫一個Segue公司我有一個自定義的表視圖細胞一些UIButtons按下時我希望他們能夠調用視圖控制器是主機表認爲,細胞在塞格斯。我在做什麼現在宣佈這樣的行爲: - (void)action;由一個UITableViewCell

在表視圖中類,然後我打電話從小區像這樣的動作:

ViewController *viewController = [[ViewController alloc] init]; 
[viewController action]; 

然而,當動作被調用它說,有沒有這樣的賽格瑞的視圖控制器,我知道是不正確的。從視圖控制器本身調用「動作」本身是完美的,而不是來自單元格。

而且,這裏是執行SEGUE代碼:

-(void)action { 
    [self performSegueWithIdentifier:@"action" sender:self]; 
} 

我怎樣才能使這項工作?

任何建議表示讚賞。

UPDATE

我只是想設置一個委託在這樣的小區:

Cell的頭文件: @class PostCellView;

@protocol PostCellViewDelegate 
-(void)action; 
@end 

@interface PostCellView : UITableViewCell <UIAlertViewDelegate, UIGestureRecognizerDelegate> 

@property (weak, nonatomic) id <PostCellViewDelegate> delegate; 

而在該小區叫我像這樣行動的主要文件: [self.delegate行動];

,並在承載表視圖我導入細胞這樣的觀點標題: #進口「PostCellView.h」

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, PostCellViewDelegate> 

然後在該視圖的主文件我有細胞行動:

-(void)action { 

    [self performSegueWithIdentifier:@"action" sender:self]; 

} 

但行動從未執行,我檢查。我從單元中調用動作時記錄了日誌,並且將一個NSLog放在動作本身上,並且從單元中調用它的日誌起作用但不是動作。

+1

你能顯示代碼的參考,你執行SEGUE? – diederikh

+0

好的,我添加了它。 – user1725710

+0

分配和初始化視圖控制器只是一個例子嗎?我不明白爲什麼你必須這樣做,因爲當單擊按鈕時,TableViewController已經存在。 – MaxGabriel

回答

1

我認爲你的問題是這樣的ViewController *viewController = [[ViewController alloc] init]; [viewController action]; 你正在啓動一個新的ViewController,而不是獲取當前的單元格。

也許你可以在小區中設置的委託,因此電池具有到ViewController

+0

我只是試圖添加單元格作爲一個委託,但我無法得到它的工作,我添加了我的代碼和問題我有原始問題。 – user1725710

+0

在cellForRowAtIndexPath中設置了cell.delegate = self? – Darren

+0

這是問題,謝謝! – user1725710

0

簡單的方法來創建一個按鈕,並調用其處理程序編程是

UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button setFrame:CGRectMake(100, 100, 80, 40)]; 
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown]; 
[self.view addSubview:button]; //self.view is the view on which you want to add button 

及其處理程序應該像這樣在您的實現文件中定義:

-(void)buttonClick:(id)sender{ 
    NSLog(@"button clicked"); 
    // your code on click of button 
} 

您可以檢查iOS documentation

希望這將有助於你找出你的問題。 :)

+0

這完全不是我所問的......我需要在自定義UITableViewCell中有一個按鈕,我已經在單元格中設置了一個IBAction,調用一個動作在視圖控制器中執行一個segue,UITableView是在 – user1725710

+0

你見過這個http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomSegues/CreatingCustomSegues.html可能會對你有幫助。 –

+0

我不需要創建自定義的Segue,我只需要調用一個從自定義UITableViewCell設置到視圖控制器的segue。 – user1725710