我有这样的登录码:
@protocol LoginDelegate
-(void)DUsername:(NSString *) username DPassword:(NSString *) password;
@end
@interface loginipad : UIViewController {
id<LoginDelegate> delegate;
IBOutlet UITextField *edusername;
IBOutlet UITextField *edpassword;
}然后在mainViewController上使用这个对象,如下所示:
@interface mainViewController : UIViewController<LoginDelegate> {并在mainViewController上调用此方法
-(void)DUsername:(NSString *) username DPassword:(NSString *) password{
userlogin=[username retain];
passlogin=[password retain];
if (!scm.isRunning) {
[scm connectToHost:@"localhost" onPort:8080];
}
}这种方法可以成功地将数据从登录模式视图解析到mainViewController,但当登录按钮单击时,我希望将进程的进度或来自mainViewController的任何消息显示到登录模式视图中(我尝试了MBPrgoressHUD,但没有成功,因为我在模式视图上使用了此登录)。
我的问题是如何将数据从mainViewController解析到这个登录模式视图?
谢谢,
对于调用该方法:
loginipad *plogin = [[loginipad alloc] initWithNibName:@"loginipad" bundle:nil];
plogin.delegate = self;
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:plogin];
plogin.title=@"Login";
[self presentModalViewController:nc animated:YES];
[nc release];
nc = nil;
[plogin release];
plogin = nil;发布于 2011-05-22 20:09:22
answer已完全编辑
你的问题会引出多个解决方案和策略。
第一:在两个类之间实现双向数据传输的一般可能性。
通过多种协议:松散耦合,但会导致令人讨厌的导入循环。我知道如何为类定义(@class)解决导入循环,但我不知道如何为协议解决这个问题
A.h:
#import "B.h"
@protocol ADelegate
-(void) adelegate:(NSString*)data;
@end
@interface A : NSObject<BDelegate>
{
id<ADelegate> delegate;
}
@endB.h:
#import "A.h"
@protocol BDelegate
-(void) bdelegate:(NSString*)data;
@end
@interface B : NSObject<ADelegate>
{
id<BDelegate> delegate;
}
@end通过单一协议:密集连接:(但没有import-loop (这是一种丑陋的工作方式)
A.h:
//no import here needed
@protocol ADelegate
-(void) adelegate:(NSString*)data;
@end
@interface A : NSObject<BDelegate>
{
id<ADelegate> delegate;
}
@endB.h:
#import "A.h"
@interface B : NSObject<ADelegate>
{
A* delegate;
}
@end通过管道/流:双向数据传输应该使用管道(无缓冲)或流(有缓冲)。这里我向您展示了一个小而简单的委托管道,但也存在一个NSPipe/NSStream
DelegatePipe.h
@protocol DelegatePipeDelegate
- dataArrived:(NSString*)data;
@end
@interface DelegatePipe : NSObject {
NSMutableArray *delegates;
}
-(void)open:(id<DelegatePipeDelegate>)d;
-(void)close:(id<DelegatePipeDelegate>)d;
-(void)send:(NSString*)data;
@endDelegatePipe.m
@implementation DelegatePipe
-(id)init
{
if(self = [super init])
{
delegates = [NSMutableArray array];
}
return self;
}
-(void) dealloc
{
[delegates release];
delegates = nil;
}
-(void) open:(id <DelegatePipeDelegate>)d
{
@synchronized(self)
{
if([delegates containsObject:d])
return;
//if([delegates count]>=2) //Pipe contains originally only 2 delegates. but a broadcaster is also nice ;)
// return;
[delegates addObject:d];
}
}
-(void) close:(id <DelegatePipeDelegate>)d
{
@synchronized(self)
{
[delegates removeObject:d];
}
}
-(void) send:(NSString *)data
{
@synchronized(self)
{
for(id<DelegatePipeDelegate> d in delegates)
[d dataArrived:data];
}
}
@end第二: KVO
KVO通常用在ModelViewController (MVC)模式中。例如:在视图中可视化数据。在本例中,network-connection-state是数据的一部分,loginipad是一个视图(和控制器)
Authentificator.h
typedef enum eAuthState
{
NOT_CONNECTED = 0,
LOGIN_FAILED,
CONNECING,
CONNECTED
} AuthState;
@interface Authentificator : NSObject {
AuthState state;
}
@property (nonatomic, assign) AuthState state;
@endAuthentificator.m
...
-(void) doAuthWithUsername:(NSString*)name password:(NSString*)pw
{
self.state = CONNECING;
//do network-stuff
}
//delegate from network. here NSURLConnection
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
//parse network-answer
BOOL success = YES;
if(success)
self.state = CONNECTED;
else
self.state = LOGIN_FAILED;
}loginipad.h
@interface loginipad : UIViewController
{
Authentificator *auth;
}
@property (nonatomic, retain) Authentificator *auth;
@endloginipad.m
@implementation loginipad
@synthesize auth;
//override setter for more comfortable use (add/removeObserver)
-(void) setAuth:(Authentificator *)a
{
@synchronized(auth)
{
[auth removeObserver:self forKeyPath:@"state"];
[auth release];
auth = a;
[auth retain];
[auth addObserver:self forKeyPath:@"state" options:0 context:nil];
}
}
-(IBAction) buttonClicked:(id)aSender
{
self.auth = [Authentificator sharedAuthentificator];
}
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if(![object isKindOfClass:Authentificator.class])
return;
AuthState state = ((Authentificator*)object).state;
NSLog(@"curState: %i",state);
//do sth with state
}
- (void)dealloc {
self.auth = nil;
[super dealloc];
}https://stackoverflow.com/questions/6087754
复制相似问题