lixiaoyu
lixiaoyu

Reputation: 378

Switching ViewControllers, but nothing happens?

I want to put the code of switching ViewControllers to a single class, which named SwitchController. But nothing happens.

Here is the code:

//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

//This is the self.window.rootViewController------RootViewController.m

//In this ViewController's view, there is a button, and on press, it will do the following thing

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

So, is there anything wrong with my code?

Why does nothing happens when I press the button?

Upvotes: 1

Views: 148

Answers (2)

Dinesh Raja
Dinesh Raja

Reputation: 8501

There may be several issues..

1) Check whether your button outlet is correctly connected to this method -(IBAction)pressed:(id)sender;

2)Add this line after @synthesize in SwitchController.m

static SwitchController *sharedSingleton = nil;

3)change your method like this

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

4) Remove this line from the -id(init) method

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

5) Check whether the button is valid. Before calling this method requestToSwitchViewController: check what it logs

UPDATE:

Try like this: (For this, you need to create a property for navigation controller in your appDelegate and synthesize it)

switch (tag)
{
    case 1:
    {
        FirstViewController *first = [[FirstViewController alloc] init];
        appDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [appDelegate.navigationController presentViewController:first animated:YES completion:nil];
    }
    .....
  .........
 }

Upvotes: 1

pedrotorres
pedrotorres

Reputation: 1232

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

Upvotes: 0

Related Questions