5

ストーリーボードを使用したXcode 4.6.1 iOS 6

私の問題はこれです

UIViewController (それ自体がナビゲーション コントローラーに埋め込まれている) の UIView に動的プロトタイプ セルを持つ UITableView があり、ある特定のセルから別のビューにセグエしたい

(誰もが私が UITableViewController を使用する必要があると提案する前に、私は UIView に他のものを持っているので、理由のためにこのように設定しています。)

セグエの作成方法がわかりません

プロトタイプ UITableViewCell からドラッグしてセグエを作成すると、生成されたすべてのセルが自動的にセグエを呼び出します。これは通常の動作であり、UITableViewController からドラッグして [self performSegueWithIdentifier:.... を呼び出してセグエを作成することで UITableViewController を使用していた場合、これを回避できます。私の didSelectRowAtIndexPathMethod から、このセグエを実行したい特定のセルのみがトリガーされます。 .

この場合、UITableViewController はありません。UIViewController サブクラスの一部である UIView の UITableView だけです。

私は遊んでいて、UITableViewからドラッグできないことを発見しました-それをさせないので、行き止まりでした。

私に残された唯一の選択肢は、UIViewController からドラッグすることでした

それで私はそれを試しました、そしてもちろんXCodeは私が持っていることを私に告げる実行セグエ行でエラーをスローします...「LocationTV」の目に見えるインターフェースはセレクターperformSegueWithIdentifierを宣言しません。LocationTv は私の tableview サブクラスです。

この状況で新しいビューを呼び出す正しい方法は何ですか?

感謝

サイモン

4

3 に答える 3

5

まずsegues間のみ使用できますUIViewControllers。したがって、同じビュー コントローラー上にある 2 つのビュー間でセグエを実行したい場合、それは不可能です。

しかし、2 つのビュー コントローラー間でセグエを実行したい場合、セグエは 1 つのビュー (最初のビュー コントローラー内) からのアクションによってトリガーされる必要があります。

したがって、あなたの場合、質問を理解すれば、カスタム内にある UITableView の最初のセルUIViewがタップされたときにセグエを実行したいと考えています。最も簡単な方法は、デリゲート メソッドが呼び出されたときUIViewにカスタムを含む UIViewController によって実装されるカスタムにデリゲートを作成するUIViewことです。セグエを実行する必要があります。短い例を次に示します。

YourCustomView.h

@protocol YourCustomViewDelegate <NSObject>

-(void)pleasePerformSegueRightNow;

@end

@interface YourCustomView : UIView {
   UITableView *theTableView; //Maybe this is a IBOutlet
}
@property(weak, nonatomic) id<YourCustomViewDelegate>delegate;

YourCustomview.m

@implementation YourCustomview
@ synthesise delegate;

//make sure that your table view delegate/data source are set properly
//other methods here maybe

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == 0) {  //or any other row if you want
       if([self.delegate respondsToSelector:@selector(pleasePerformSegueRightNow)]) {
          [self.delegate pleasePerformSegueRightNow];
       }
    }
} 

YourTableViewController.h

@interface YourTableViewController : UIViewController <YourCustomViewDelegate> {
  //instance variables, outlets and other stuff here
}

YourTableViewController.m

@implementation YourTableViewController

-(void)viewDidLoad {
  [super viewDidLoad]; 
  YourCustomView *customView = alloc init....
  customView.delegate = self;
}

-(void)pleasePerformSegue {
  [self performSegueWithIdentifier:@"YourSegueIdentifier"];
}

デリゲートに対して任意のメソッドを作成したり、動作をカスタマイズしたりできます。これは、その方法の簡単な例です。

于 2013-06-14T07:54:10.870 に答える
3

My Solution

I ended up using a delegation pattern

I made a segue dragging from the my UIViewController - specifically dragging from the viewController icon (the orange circle with a white square in it - from the name bar thats under the view in the storyboard - although you could also drag from the sidebar ) to the view that i wanted to segue to.

I needed to trigger this segue from a table view cell on a table view.

TableView Bit

So i declared a protocol in my tableview header file - which is called LocationTV.h - as follows

@protocol LocationTVSegueProtocol <NSObject>

-(void) makeItSegue:(id)sender;

@end

Below that I declare a property to hold my delegate

@property (nonatomic, strong) id<LocationTVSegueProtocol> makeSegueDelegate;

To actually trigger the segue i called the makeItSegueMethod on my makeSequeDelegate in my didSelectRowAtIndexPath method

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

switch (indexPath.section) {
        DLog(@"selected row %d",indexPath.row);
    case dLocation:
    {
        if(indexPath.row == 2){

            [_makeSegueDelegate makeItSegue:self];

        } else if (indexPath.row == 7){

UIViewController Bit

and set up my UIViewController (named MultiTableHoldingVC) as implementing that protocol

@interface MultiTableHoldingView : UIViewController    
                                  <EnviroTVProtocol,LocationTVSegueProtocol> {

}

Below that i declared the protocol method in the list of my classes methods (although i'm not sure that is necessary as the compiler should know about the method as the decalration of implementing a protocol is essentially a promise to implement this method)

-(void) makeItSegue:(id)sender;

And then over in the implementation file of my UIViewController i wrote the method which essentially just calls preformSegueWithIdentifier

-(void) makeItSegue:(id)sender{ 
    [self performSegueWithIdentifier:@"ChooseCountryNow" 
                              sender:sender];   
}

And to link it all together,as in the header file I had declared my instance of the tableView as follows

@property (strong, nonatomic) IBOutlet LocationTV *dsLocationTV;

I had to set that tables views delegate property to be self - which I did in my UIViewControllers -(void)ViewDidLoad method

_dsLocationTV.makeSegueDelegate = self;

It all seems a bit of a kludge calling a method to call a method and allprog suggestion is simpler (I cant for the life of me work out why it threw up errors for me) but this works just fine . Thanks to both allprog and danypata for their suggestions.

Hope this is helpful to someone out there

于 2013-06-19T09:41:44.440 に答える
0

performSegueWithIdentifier:UIViewController クラスのメソッドです。UITableView インスタンスで呼び出すことはできません。ビュー コントローラーに UITableViewDelegate プロトコルを実装させ、それを UITableView のデリゲートとして設定します。

別のオプションは、セグエを使用しないことです。同じデリゲート メソッドで次の操作を行います。

OtherViewController ov = [[OtherViewController alloc] init<<some initializer>>];
// Or in case of storyboard:
OtherViewController ov = [self.storyboard instantiateViewControllerWithIdentifier:@"ovidentifier"];

// push view controller
[self.navigationController pushViewController:ov animated:YES];

デリゲート オブジェクトがビュー コントローラーと異なる場合、最も簡単な解決策は、次のように、ビュー コントローラーへの参照を保持する弱いプロパティをデリゲートのクラスに追加することです。

@property (weak) UIViewController *viewController;

それをviewDidLoadviewControllerの

- (void) viewDidLoad {
    self.tableView1.viewController = self;
}

tableView1プロパティが次のように宣言されていることを確認してください。

@property (IBACTION) (weak) SpecialTableView *tableView1;

自分でコードを書くよりも、ストーリーボードを使用する方が面倒な場合があります。

于 2013-06-14T07:52:46.400 に答える