如何从iOS中的NSObject类返回到UIViewController

时间:2016-07-05 08:29:49

标签: ios objective-c iphone uiviewcontroller uiwebview

我有UIViewController班。在那个课程中,我实现了UIWebView。在WebView上,我从CocoaHttpServer获取数据并加载html页面。在CocoaHTTPServer中有一个NSObject类。在那个课程中,我有一个方法可以弹出到上一个屏幕。如何弹出以查看来自NSObject类的控制器。

这是我的NSObject班级

 if([[array lastObject] compare:@"back-button-clicked"] == NSOrderedSame) {
    [self handleBackActionRequest];
}

-(void)handleBackActionRequest
{
    PGSoftApViewController *softAP = [[PGSoftApViewController alloc] initWithNibName:nil bundle:nil];
    [softAP webToBackNativeCall];
}

这是我的ViewController课程:

-(void)webToBackNativeCall
{
    [self.navigationController popViewControllerAnimated:YES];
}

这不起作用。我能够插入 webToBackNativeCall 方法。但它没有发生任何事情。

我必须回去。我怎么能帮助我。

2 个答案:

答案 0 :(得分:0)

在你的NSObject.h类中添加这样的协议

@protocol nsobjectDelegate <NSObject>
-(void)navigateToBackVC;
@end

@interface objectClass:NSObject 
//your other code
@property (nonatomic,weak) id<nsobjectDelegate> objDelegate;
@end

在你的NSObject .m类

-(void)webToBackNativeCall
{
    // this method you already have 
    // other code if you want
    [self.objDelegate navigateToBackVC];
}

在你查看控制器.h类

@interface objectClass:NSViewcontroller <nsobjectDelegate> // delegate of NSObject include it in VC when you want to call that methods

在viewDidLoad中查看控制器.m文件或​​者在NSObject类的setAidar或DidAppear方法初始化对象中设置委托

NSObjectClass *objeObject = [[NSObjectClass alloc] init]; 
objeObject.objDelegate = self;

实施此方法

-(void)navigateToBackVC
{
    [self.navigationController popViewControllerAnimated:YES];
}

这是协议和委托的一般概念。

希望这对你有所帮助。

答案 1 :(得分:0)

在ViewController的viewDidLoad中

[[myNSOBjectClass alloc] startViewController:self];

在NSObject类

@interface myNSOBjectClass{
  //Global access to the view controller. 
  UIViewController *viewController; 
}

@implementation myNSOBjectClass

//function that the caller calls to pass their UIViewController

- (void)startViewController:(UIViewController *)callerViewController{
    viewController = [[UIViewController alloc]init];
   //puts the caller's view controller to the global member.
    viewController = callerViewController;  
}