1

NSObjectクラスである別のクラスからperformSegueWithIdentifierを呼び出そうとしていますが、次のエラーが発生します。

キャッチされなかった例外「NSInvalidArgumentException」が原因でアプリを終了しています。理由:

 'Receiver (<DerinlikView: 0x95b1ac0>) has no segue with identifier 'DerinlikToPali''

私のコードは次のとおりです。

Class1(NSObject):

    DerinlikView *derinlik = [DerinlikView alloc];
    [derinlik performSegue];

Class2(UIViewController):

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

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ( [[segue identifier] isEqualToString:@"DerinlikToPali"]  )
    {
        Palinolojik *pali = segue.destinationViewController;
    }
}
4

3 に答える 3

7

私は同じ問題を抱えていましたが、NSNotificationCenterを使用して問題を解決することができました。

NSObjectクラスの.mファイルに次を追加しました。

[[NSNotificationCenter defaultCenter] postNotificationName:@"performsegue"
                                                    object:nil];

ViewControllerクラスの.mファイル内-

1.-(void)viewDidLoadに追加しました:

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

2.-(void)viewDidUnloadに追加しました:

[[NSNotificationCenter defaultCenter] removeObserver:self];

3.メソッドperformsegueNotificationを作成しました:

-(void)performsegueNotification:(NSNotification *) notif
{

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

}
于 2013-05-16T21:47:13.643 に答える
2

私はあなたのストーリーボードやあなたのセグエに何も悪いことはないと信じています。

問題はこの行である必要があります=

DerinlikView *derinlik = [DerinlikView alloc];

Class2に新しいviewcontrollerポインターを割り当てると、ビューに接続されているセグエを見つけることができません。

解決策は、開始したClass2のポインターをNSObjectクラスに渡すことです。

class1.hで:

@interface Class1 : NSObject
{
    Class2 *viewController;
}

@property (nonatomic,strong) Class2 *viewController;

class1.mで

@synthesize viewController;

[self.viewController performSegue];

class2.mの場合:

Class1 *callNsObject= [[Class1 alloc] init];
UIViewController *currentVC=self;//this is the part you need the pass current viewcontrollers pointer to your nsobject class depends on your project thee are multiple ways of doing it.
callNsObject.viewController=currentVC;

重要:Class2ビューコントローラーの階層がわからないため、この行で現在のビューコントローラーポインターUIViewController *currentVC=self;に変更する必要があります。self

たとえば、ナビゲーションベースのアプリの場合、次の方法で現在のViewControllerを取得できます。 UIViewController *currentVC = self.navigationController.visibleViewController;

また、Class1メソッドを呼び出す前に、Class2のビューがスタックにプッシュされると想定しています。

于 2013-03-18T14:49:00.113 に答える
0

The error note says that "DerinlikToPali" was not found in StoryBoard.

Go to your storyBoard, select the segue and check it's Attribute Inspector (menu View - Utilities - Show Attribute Inspector). Make sure the identifier is the same string you use at performSegueWithIdentifier.

Update (after looking at SampleProject):

  • SampleClass *sample = [SampleClass alloc] should be "alloc init" in general, so you not only allocate memory, but also complete the full object initialization.
  • SampleClass methodToSegue() doesn't create ViewController object as you expected. First see above "alloc init", but in this case you need more (below). Otherwise you will NOT have a valid UIViewController object. Note that you got to first give it a name at storyBoard, so that you can create it (using that name). Alternative is to use XIB instead of storyBoard, in which case you use initWithNibName:bundle:

    ViewController *vc = [[UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil] instantiateViewControllerWithIdentifier:@"viewController"];

  • Biggest problem is logical and I hope it's only within this SampleProject: objectA is created, creates objectB, which creates objectA and calls it's method... except that objectA creation creates objectB again --> and we have a forever loop and something will crash.

Anyway, my updated answer is that your object creation was not completed before you tried to trigger the segue. Especially "ViewController" was not yet an actual UIViewController defined at storyBoard and thus did not have the expected segue.

于 2013-03-18T14:20:03.330 に答える