1

我想将切换 ViewControllers 的代码放到一个名为 SwitchController 的类中。但什么也没有发生。

这是代码:

//AppDelegate.m

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.window.backgroundColor = [UIColor whiteColor];

        RootViewController *root = [[RootViewController alloc] init];
        [self.window setRootViewController:root];

        [self.window makeKeyAndVisible];
        return YES;
}

//SwitchController.h

@interface SwitchController : NSObject

@property (strong,nonatomic) RootViewController *rootViewController;

+(SwitchController *)sharedInstance;
-(void)requestToSwitchViewController:(NSInteger)tag;

@end

//SwitchController.m

#import "SwitchController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"

@implementation SwitchController

@synthesize rootViewController = _rootViewController;

-(id)init
{
    self = [super init];
    if (self == nil)
    {
        _rootViewController = [[RootViewController alloc] init];
    }
    return self;
}

+(SwitchController *)sharedInstance
{
    static SwitchController *sharedSingleton = nil;
@synchronized([SwitchController class])
{
    if(sharedSingleton == nil)
    {
        sharedSingleton = [[self alloc] init];
    }
}
return sharedSingleton;
}

-(void)requestToSwitchViewController:(NSInteger)tag
{
    switch (tag)
    {
        case 1:
        {
            FirstViewController *first = [[FirstViewController alloc] init];
            [self.rootViewController presentViewController:first animated:YES completion:nil];
        }
            break;
         .........  //Do something else
        default:
            break;
    }
}

@end

//这是self.window.rootViewController------RootViewController.m

//在这个ViewController的视图中,有一个按钮,按下它会做下面的事情

    - (IBAction)pressed:(id)sender
    {
        [[SwitchController sharedInstance] requestToSwitchViewController:[(UIButton *)sender tag]];
    }

那么,我的代码有什么问题吗?

为什么当我按下按钮时没有任何反应?

4

2 回答 2

1

可能有几个问题。。

1)检查您的按钮插座是否正确连接到此方法-(IBAction)pressed:(id)sender;

@synthesize2)在in之后添加这一行SwitchController.m

static SwitchController *sharedSingleton = nil;

3)像这样改变你的方法

+(SwitchController *)sharedInstance
{
    if(sharedSingleton == nil)
    {
        sharedSingleton = [[self allocWithZone:NULL] init];
    }
  return sharedSingleton;
}

-id(init)4)从方法中删除这一行

// You do not need to re-initialize the root view controller
_rootViewController = [[RootViewController alloc] init];

5) 检查按钮是否有效。在调用此方法之前requestToSwitchViewController:检查它记录的内容

更新:

试试这样:(为此,您需要create a property for navigation controller在您的appDelegate和综合它)

switch (tag)
{
    case 1:
    {
        FirstViewController *first = [[FirstViewController alloc] init];
        appDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [appDelegate.navigationController presentViewController:first animated:YES completion:nil];
    }
    .....
  .........
 }
于 2012-11-22T12:02:07.987 回答
0

I guess you're just defining the viewController, you need to define the View too. they're different things.

于 2012-11-22T10:38:17.373 回答