我遇到了上周的问题。我的要求是,我必须创建一个自定义 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
当我在 Button 单击时调用此方法时:
#import "Method_Action_class.h"
Method_Action_class *demo = [[Method_Action_class alloc] init];
[demo login_method_called];
注意:Method_Action_class
是我的NSObject
类型类
这里的代码在没有警告/错误的情况下成功运行,但没有导航到另一个类。
请帮帮我。
最佳答案
在您的按钮点击操作中,您必须发送您的 UINavigationController
和当前的 ViewController
。因为 NSObject
类没有找到那个 Controller 。
在您的按钮操作中放入此代码:
[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
并构建您的代码,它已成功导航。 :)
关于ios - 如何从 iOS 中的 NSObject 类导航到 ViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28600615/