【问题标题】:How to navigate to a ViewController from NSObject Class in iOS?如何从 iOS 中的 NSObject 类导航到 ViewController?
【发布时间】:2015-02-19 07:18:02
【问题描述】:

我上周遇到了一个问题。我的要求是,我必须创建一个自定义 NSObject 类,我必须在其中为所有按钮单击操作编写所有导航方法。当我单击一个按钮时,按钮操作将根据单击和导航从该自定义 NSObject 类中检索导航方法。但它不工作。我正在实现的内容,我在下面分享我的代码:

这是我的Method_Action_class.h 班级

   #import <Foundation/Foundation.h>
   @interface Method_Action_class : NSObject
   -(void)login_method_called;
   @end

这是我的Method_Action_class.m 班级

   #import "Method_Action_class.h"
   #import "Home_ViewController.h"

   @implementation Method_Action_class

   -(void)login_method_called
   {
       UIWindow *window=[UIApplication sharedApplication].keyWindow;
       Home_ViewController *home = [[Home_ViewController alloc] initWithNibName:@"Home_ViewController" bundle:nil];
       [window.rootViewController.navigationController pushViewController:home animated:YES];

   }
   @end

当我在按钮点击上调用这个方法时:

   #import "Method_Action_class.h"

   Method_Action_class *demo = [[Method_Action_class alloc] init];
   [demo login_method_called];

注意:Method_Action_class 是我的 NSObject 类型类

这里的代码运行成功,没有警告/错误,但没有导航到另一个类。

请帮帮我。

【问题讨论】:

  • 嘿伙计,我也面临这种类型的问题。但我找到了解决方案。我会分享我的代码,请检查:)
  • 请。感谢您的回复

标签: ios uinavigationcontroller


【解决方案1】:

在您的按钮单击操作中,您必须发送您的UINavigationController 和当前ViewController。因为NSObject 类没有找到那个控制器。

在您的按钮操作中输入以下代码:

[demo login_method_called:self.navigationController withCurrentViewController:self];

在您的 NSObject .h 类中输入以下代码:

#import <Foundation/Foundation.h>
#import "Home_ViewController.h"

@interface Method_Action_class : NSObject

- (void)login_method_called:(UINavigationController*)navigation  withCurrentViewController:(UIViewController*) controller;
@end

在您的 NSObject .m 类中输入以下代码:

#import "Method_Action_class.h"

@implementation Method_Action_class

-(void)login_method_called:(UINavigationController*)navigation  withCurrentViewController:(UIViewController*) controller
{
   Home_ViewController *home = [[Home_ViewController alloc] initWithNibName:@"Home_ViewController" bundle:nil];
   [navigation pushViewController:home animated:YES];
}
@end

并构建您的代码,它已成功导航。 :)

【讨论】:

    【解决方案2】:

    请使用 NSNotificationCenter 并使用该控制器的 PushView。或者您可以使用自定义委托。对此您可以参考

    [1]: https://stackoverflow.com/questions/2191594/send-and-receive-messages-through-nsnotificationcenter-in-objective-c

    【讨论】:

    • 感谢您的回答
    • 好的,我试试,然后回复你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 2020-11-16
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多