1

デリゲートの背後にある論理を理解していると思います。私はそれを使うのにもっと問題がありました。いくつのステップが含まれますか?既存のデリゲートを使用する必要がありますか?または、自分のものを使用できますか?

私の例では、同じタイプの多くのビュー(オブジェクト/ビューコントローラー)を作成するAppDelegateを取得しました。各ビューは、何らかの方法でAppDelegateのメソッドを呼び出して、それ自体を閉じる必要があります。これは、ビュー内のボタンがタッチされたときに発生します。メソッド呼び出しには、ビュー(自己)の参照が含まれます。

これまでのところ、他の言語のレスポンダー、イベントリスナーなどから知っています。使い方はとても簡単です。

誰か助けてくれませんか。Webにたくさんのコードがある大規模な例を見つけました。ObjectiveCで親を呼び出すのはそれほど難しいことではありません。

4

3 に答える 3

1

必要なものを取得する簡単な方法は、1つのビューから始めることです。次に、お互いのビューをモーダルに表示します。ビューのボタンが押されたときに実行します

[self dismissModalViewControllerAnimated:YES];

そして、これは私がiPhoneの開発を始めていたときに私が作ったもので、代理人を助けるかもしれません

Delegates

//In parent .m file:
//assign the delegate
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"segueName"])
    {
        childController *foo = segue.destinationViewController;
        foo.delegate = self;
    }

}

//implement protocol method(s):
- (void) methodName:(dataType*) dataName
{
    //An example of what you could do if your data was an NSDate
    buttonLabel.titleLabel.text = [[date description] substringToIndex:10];
}

//In parent .h file:
//import child header
#import "ChildName.h"

//indicate conformity with protocol
@interface ParentName : UIViewController <ChildNameDelegate>

//In child .h file
//declare protocol
@protocol ChildNameDelegate
- (void) methodName:(dataType*) dataName;
@end

//declare delegate
@property (unsafe_unretained, nonatomic) id<ChildNameDelegate> delegate;


//In child .m file
//synthesize delegate
@synthesize delegate; 

//use method
- (IBAction)actionName:(id)sender 
{
    [delegate methodName:assignedData];
}
于 2012-06-27T15:36:02.270 に答える
1

あなたはあなた自身を作成することができます:

MyView1.hの場合:

@class MyView1;

@protocol MyView1Delegate <NSObject>

- (void)closeMyView1:(MyView1 *)myView1;

@end

@interface MyView1 : NSObject
{
    id<MyView1Delegate> _delegate;
}

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

...

@end

MyView1.mの場合:

@interface MyView1

@synthesize delegate = _delegate;

...

// The method that tells the delegate to close me
- (void)closeMe
{
    ....
    if ([_delegate respondsToSelector:@selector(closeMyView1:)])
    {
        [_delegate closeMyView1:self];
    }
}

@end

AppDelegate.hの場合:

#import "MyView1.h"

@interface AppDelegate <MyView1Delegate>
{
    MyView1 *_myView1;
}

...

@end

AppDelegate.mの場合:

- (void)someCreateViewMethod
{
    _myView1 = [[MyView1 alloc] initWithFrame:NSMakeRect(0, 0, 100, 200)];
    [_myView1 setDelegate:self];
    ...
}
于 2012-06-27T15:36:27.603 に答える
1

私はあなたがこれのために使うべきだと思いますNSNotificationCenter

あなたの中でAppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
...
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(buttonPushed:) name:@"ButtonPushedNotification" object:nil];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
...
...
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

これは、通知が発生したときに呼び出されるセレクターです(まだAppDelegate.mにあります)

- (void)buttonPushed:(NSNotification *)notification {
NSLog(@"the button pushed...");
}

ボタンが押されたときのViewController.m(メソッド内)で、次のような通知を投稿する必要があります。

{
...
[[NSNotificationCenter defaultCenter] postNotificationName:@"ButtonPushedNotification" object:nil];
...
}
于 2012-06-27T16:32:23.597 に答える