【问题标题】:own delegate - function work in 50%, something is wrong自己的委托 - 函数工作在 50%,有问题
【发布时间】:2012-01-20 13:05:45
【问题描述】:

我有自己的班级代表。

MainMenuViewDelegate

#import <Foundation/Foundation.h>
@class MainMenuView;

@protocol MainMenuViewDelegate <NSObject>

-(void) mainMenuViewLibrary:(MainMenuView*)controller withString:(NSString*)string;

@end

MainMenuView.h

#import <UIKit/UIKit.h>
#import "WorkspaceView.h"
#import "MainMenuViewDelegate.h"

@interface MainMenuView : UIView
@property (nonatomic,weak) id <MainMenuViewDelegate> delegate;
....
@end

MainMenuView.m

@implementation MainMenuView
@synthesize delegate;
...
-(void)library:(id)sender{
//test
NSLog(@"it's work");
NSString *string = @"some text";
[delegate mainMenuViewLibrary:self withString:string];
NSLog(@"finish");

}

WorkspaceView.h

#import <UIKit/UIKit.h>
#import "MainMenuViewDelegate.h"
@interface WorkspaceView : UIView <MainMenuViewDelegate> {
int menuStatus;
UILabel *label;
}
@property int menuStatus;
@property (nonatomic, retain) UILabel *label;
@end

WorkspaceView.m

#import "WorkspaceView.h"
#import "MainMenuView.h"
@implementation WorkspaceView
@synthesize menuStatus;
@synthesize label;
....
-(void) mainMenuViewLibrary:(MainMenuView*)controller withString:(NSString*)string{
[label setText: string];
}

@end

当我按下 btnLibrary 并调用 -(void)library:(id)sender 函数时出现问题。 控制台打印it's work,然后我的委托函数被调用,但我没有看到我的标签(放置在WorkspaceView)和完成控制台打印finish有任何变化。

【问题讨论】:

  • [delegate mainMenuViewLibrary:self withString:string]; 被主线程调用了吗?
  • @ChrisWagner,我项目中的主类是ViewController,这里初始化了MainMenuView和WorkspaceView对象。

标签: objective-c class methods delegates


【解决方案1】:

您的任何对象都未设置为委托,因此委托为 nil - 发送到 nil 的消息被静音。什么都没发生。

如果没问题,把NSLog放进去:

-(void) mainMenuViewLibrary:(MainMenuView*)controller withString:(NSString*)string;

在您的 WorkspaceView 中检查该方法是否被调用。

希望对您有所帮助。

【讨论】:

  • 很有可能,我假设这已经完成了,但要验证是件好事。
  • 是的,我没有这个,但我怎么添加这个?在哪里?请帮忙。当我单击 btnLibrary 时,我想设置标签文本的新值。我是初学者。谢谢。我试图通过这段代码来理解代表; )
  • 在分配 MainMenuView 部分的 WorkspaceView 中,分配委托。示例: MainMenuView *m = [[MainMenuView alloc] init]; [m setDelegate:self];
  • @Rafał Sroka,但我在ViewController.mMainMenuViewWorkspaceView 中分配MainMenuViewViewController 的子类。请看ViewController的这段代码:pastebin.com/qEti0Bu0
  • @RafałSroka 我现在看到你编辑你的答案。所以,我输入了NSLog,我的委托函数没有被调用。
猜你喜欢
  • 2016-07-18
  • 2012-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-05
  • 2010-12-29
相关资源
最近更新 更多