0

我是iOS世界的新手,所以请忽略显而易见的。

我将 viewController(HelpViewController) 推到另一个 viewController(MainViewController) 之上。当 HelpViewController 中发生特定操作时,我想更新 MainViewController 中的变量。我知道为此我需要使用委托。这是我的代表标题...

@protocol ViewControllerDelegate <NSObject>
@required
- (void) switchToggled:(BOOL)status;
@end
// Protocol Definition ends here
@interface ViewDelegate : NSObject
{
    // Delegate to respond back
    id <ViewControllerDelegate> _delegate;
}
@property (nonatomic,strong) id delegate;

-(void)sendMessage:(BOOL)status; // Instance method

@end

和实施...

@implementation ViewDelegate

@synthesize delegate;

-(id)init {

    self = [super init];

    return self;

}

-(void)sendMessage:(BOOL)status
{
    [delegate switchToggled:status];
}

- (void)dealloc
{
    [super dealloc];
}
@end

所以现在如果我想实现协议 ViewControllerDelegate 我需要在 MainViewController 中指定,我这样做如下 -

MainViewController <ViewControllerDelegate>

#pragma mark - ViewControllerDelegate delegate
-(void)switchToggled:(BOOL)status{
    NSLog(@"Switch Toggled(%d) Message passed to MainViewController",status);
}

我的问题是如何指定对象,委托属性需要指向哪个,以便它可以返回到 MainViewController 的“switchToggled”。

我做的一种方法是在 HelpViewController 中拥有如下属性 -

MainViewController.m
HelpViewController *helpVC = [[HelpViewController alloc] init];
helpVC.mainView = self;
[self.navigationController pushViewController:helpVC animated:YES];
[helpVC release];

HelpViewController.h
    @property (nonatomic) MainViewController *mainView;
HelpViewController.m
@synthesize mainView;
ViewDelegate *myDelegate = [[ViewDelegate alloc] init];
// assign delegate
myDelegate.delegate = mainView;
[myDelegate sendMessage];
[myDelegate release];

这是正确的实施方式还是有更好的方式来实现这一点,或者我完全错了。

谢谢

4

2 回答 2

1

你应该做:

// HelpViewController.h
@protocol HelpDelegate

- (void)switchToggled:(BOOL)status;

@end


// HelpViewController.m
@interface HelpViewController : UIViewController

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

- (id)initWithDelegate:(id<HelpDelegate>)delegate

@end

@implementation HelpViewController
- (id)initWithDelegate:(id<HelpDelegate>)delegate
{
    if (self = [super init])
    {
        self.delegate = delegate;
    }
}

- (void)sendMessage:(BOOL)status
{
    [self.delegate switchToggled:status];
}


// MainViewController.h 
#import "HelpViewController.h"

@interface MainViewController.h : UIViewController <HelpDelegate>


// MainViewController.m

- (void)someMethod
{
    HelpViewController* viewController;
    viewController = [HelpViewController alloc] initWithDelegate:self];
    ...
}

#pragma mark - Help Delegate

- (void)switchToggled:(BOOL)status
{
    ...
}
  • 给委托起一个明确它属于哪个类的名称。
  • You don't need the extra class/files for ViewDelegate/ViewControllerDelegate. Just define the delegate in header of class it belongs to: HelpViewController.n in this case.
  • Similar: Implement the delegate method switchToggled: in the real class MainViewController, and not in the extra/unnecessary class ViewDelegate.
  • The purpose of delegates is to avoid class dependencies. By including MainViewController in HelpViewController you create such a dependency. This is not necessary as I show, and is wrong design.
  • You were also creating a circular dependency, because MainViewController already needed HelpViewController in order to show it, and now they need each other the other way around for sending the event.
  • Alternatively you can make HelpViewController's delegate public, have an init without argument, and expect users to set it with helpViewController.delegate = self; or something. But this would only make sense when the delegate being set is optional (which don't seems the case here, so adding it to the init method is appropriate).
于 2013-08-29T07:53:20.370 回答
0

我告诉你我会做什么:

1)协议定义没问题,但不要创建类ViewDelegate,所以:

//ViewControllerDelegate.h
@protocol ViewControllerDelegate <NSObject>
@required
- (void) switchToggled:(BOOL)status;
@end

2)您在 MainViewController 中的委托方法的实现是好的。

3)现在......重点:

    //interface
@interface HelpViewController : UIViewController //or whatever superclass..
{
    id <ViewControllerDelegate> _delegate;
}
@property (nonatomic,strong) id<ViewControllerDelegate> delegate;

@end
//implementation
@implementation HelpViewController

- (void)someMethodWhichCallsTheDelegate
{
    //do something
    ...
    // call delegate
    //if switchToggled: were optional then add the following
    //if ([self.delegate respondToSelector:@selector(switchToggled:)]) {
    [self.delegate switchToggled:status];
}
@end

4)现在您必须分配代表:

//MainViewController.m
HelpViewController *helpVC = [[HelpViewController alloc] init];
helpVC.delegate = self;
[self.navigationController pushViewController:helpVC animated:YES];
[helpVC release];

就是这样!

顺便说一句:如果此委托仅与HelpViewController在定义类接口的位置添加协议定义相关,则无需创建单独的头文件。如果相反,协议是“全局的”,那么单独声明它可能会有一些意义。

于 2013-08-29T07:51:55.327 回答