【问题标题】:Delegates and protocols objective c代表和协议目标 c
【发布时间】:2010-10-28 11:40:52
【问题描述】:

我遵循了这个论坛上给出的一些建议,但我仍然遇到困难

我有以下:

    #import <UIKit/UIKit.h>


    @protocol UIViewForWheelProtocol

    - (void) returnImageNumber:(int)imgNum;

    @end


    #import <UIKit/UIKit.h>
    #import "UIViewForWheelProtocol.h";


    @interface UIViewForWheel : UIView {
        id<UIViewForWheelProtocol> delegate;
    }

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



    @implementation UIViewForWheel

    @synthesize delegate;

    -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
       int num =1 ;
       [self.delegate returnImageNumber:num];
    }

#import <UIKit/UIKit.h>
#import "UIViewForWheel.h"
#import "UIViewForWheelProtocol.h"


@interface MainMenu : UIViewController <UIViewForWheelProtocol> {

}

-(void) returnImageNumber:(int)whichImage;

@end


    #import "MainMenu.h"


    @implementation MainMenu

    - (void) returnImageNumber:(int)whichImage
    {
        NSLog(@"HI %i", whichImage);
    }

HI 1 没有被显示,因为虽然它会转到 touchesMoved 函数,但不会转到 MainMenu 类中的 returnImageNumber。

谁能解释一下我做错了什么?

【问题讨论】:

  • 您是否曾将 MainMenu 实例指定为 UIViewForWheel 的委托?
  • 我也没有看到MainMenu的接口声明...那在哪里?
  • @Marcelo,显然没有...请问我该怎么办?
  • 好吧,您可以轻松编辑旧问题,而不是添加新问题...

标签: iphone objective-c delegates protocols


【解决方案1】:

确保您已为 UIViewForWheel 手动分配代理,并且 MainMenu 符合该协议 UIViewForWheelProtocol

【讨论】:

  • 像这样:UIViewForWheelProtocol p1 = [[MainMenu alloc] init]; myUiViewForWheel.delegate = p1;
  • MainMenu* p1 = [[MainMenu alloc] init]; myUiViewForWheel.delegate = p1;id&lt;UIViewForWheelProtocol&gt; p1 = [[MainMenu alloc] init]; myUiViewForWheel.delegate = p1;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多