0

カスタムデリゲートを作成しました:

例.h:

@protocol DismissExamplePopoverDelegate
- (void) dismissExamplePopover;
- (int) getUserID;
@end


@interface Example : UIViewController{
    id<DismissExamplePopoverDelegate> delegate;
}

@property (nonatomic, assign) id<DismissExamplePopoverDelegate> delegate;

これは、次のように Example.m で呼び出されます。

[[self delegate] getUserID];

私のmaincontroller.hで:

#import "Example.h"

@interface MainScreen : UIViewController<DismissExamplePopoverDelegate>

maincontroller.m:

-(int) getUserID
{
    return 100;
}

ビュー Example は、次のメソッドによって呼び出されます。

ExampleController = [[Example alloc] initWithNibName:@"Example" bundle:nil];
ExamplePopoverController = [[UIPopoverController alloc] initWithContentViewController:ExampleController];

[ExampleController setDelegate:self];
ExamplePopoverController.popoverContentSize = CGSizeMake(600, 480);

if ([ExamplePopoverController isPopoverVisible]) {
        [ExamplePopoverController dismissPopoverAnimated:YES];
} else {
    CGRect popRect = CGRectMake((self.EditExampleSelectAddButtonProperty.frame.origin.x),
                                    (self.EditExampleSelectAddButtonProperty.frame.origin.y),
                                    (self.ExampleSelectAddButtonProperty.frame.size.width),
                                    (self.ExampleSelectAddButtonProperty.frame.size.height));
    [ExamplePopoverController presentPopoverFromRect:popRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

[[self delegate] getUserID]完全に機能する以外の関数を配置すると、 viewdidload100が返されます。その中viewdidloadで0を返します。

私が達成したいのは、ポップオーバーがロードされたときに自動的に呼び出されるデリゲートです。viewdidload は最適な場所ですか、それとも別の場所にありますか

4

3 に答える 3

0

実際にデリゲート プロパティを MainController に割り当てていることを確認してください。たとえば、例を初期化した場合は常に、delegate プロパティを設定する必要があります。

Example *example = [[Example alloc] init];
example.delegate = self; // Use this if you're initializing Example in MainController

MainController で Example を初期化していない場合は、「self」の代わりに MainController インスタンスを使用します。

于 2013-06-07T02:19:44.713 に答える
0

何人かの人々がこれを言っていますが、解決策について十分に明確ではありません.

これを行う代わりに:

ExampleController = [[Example alloc] initWithNibName:@"Example" bundle:nil];
ExamplePopoverController = [[UIPopoverController alloc] initWithContentViewController:ExampleController];

[ExampleController setDelegate:self];

これを行う:

ExampleController = [[Example alloc] initWithNibName:@"Example" bundle:nil];
[ExampleController setDelegate:self];

ExamplePopoverController = [[UIPopoverController alloc] initWithContentViewController:ExampleController];

何が起こっているかというと、-[UIPopoverController initWithContentViewController:]その を使用してcontentViewControllerが呼び出されます。その時点ではデリゲートをまだ設定していないため、デリゲートはであり、 、、またはを返すときに任意のメソッドを呼び出そうとしています。view-[Example viewDidLoad]nilnilnil00.0

于 2013-06-07T12:11:58.193 に答える
-1

Example.m に次の行を追加する必要があります。

[self setDelegate:delegate];

また、する必要があります@synthesize delegate;

それが役立つことを願っています)

于 2013-06-07T11:08:22.607 に答える