Pregunta

Estoy tratando de aprender Objective-C y se han topado con un problema con empujando un nuevo controlador de vista cuando se hace clic en un botón. Tengo un interruptor controlador de vista de que es responsable de empujar el archivo settings.nib cuando pulso los ajustes de la barra de herramientas botón. Aquí está el código de la ViewController:

//
//  ViewController.h
//  iReader
//
//  Created by Vivek Manjunath on 22/08/10.
//  Copyright (c) 2010 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>
@class ReaderMain;
@class Settings;
@class GoogleClientLogin;

@interface ViewController : UIViewController {

    ReaderMain *readerMain;
    Settings *settings;
    GoogleClientLogin *gClientLogin;
    UIBarButtonItem *mSignInOutButton;
    UIBarButtonItem *mRefresh;

}

@property (nonatomic, retain) ReaderMain *readerMain;
@property (nonatomic, retain) Settings *settings;
@property (nonatomic, retain) GoogleClientLogin *gClientLogin;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *signInOutButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *refreshButton;

-(IBAction) switchViews:(id)sender;
@end

archivo ViewController.m

//
//  ViewController.m
//  iReader
//
//  Created by Vivek Manjunath on 22/08/10.
//  Copyright (c) 2010 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"
#import "ReaderMain.h"
#import "Settings.h"


@implementation ViewController
@synthesize readerMain;
@synthesize settings;
@synthesize signInOutButton = mSignInOutButton;
@synthesize refreshButton =mRefresh;
@synthesize gClientLogin;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    ReaderMain *readerController= [[ReaderMain alloc] initWithNibName:@"ReaderMain" bundle:nil];
    self.readerMain = readerController;
    [self.view insertSubview:readerController.view atIndex:0];
    [readerController release];
    [super viewDidLoad];
}

-(IBAction)switchViews:(id)sender
{
    if(self.settings ==nil)
    {   
        //[mSignInOutButton setTitle:@"SignIn"];
        Settings *googleController = [[Settings alloc]initWithNibName:@"Settings" bundle:nil];
        self.settings = googleController;

        [googleController release];
    }
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:1.00];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    if(readerMain.view.superview ==nil)
    {   
        [mSignInOutButton setTitle:@"Settings"];
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:NO];
        [readerMain viewWillAppear:YES];
        [settings viewWillDisappear:YES];
        [settings.view removeFromSuperview];
        [self.view insertSubview:readerMain.view atIndex:0];
        [settings viewDidDisappear:YES];
        [readerMain viewDidAppear:YES];
        //[mSignInOutButton setTitle:@"SignOut"];

    }

    else 
    {   
        [mSignInOutButton setTitle:@"Back"];
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:NO];
        [settings viewWillAppear:YES];
        [readerMain viewWillDisappear:YES];

        [readerMain.view removeFromSuperview];
        [self.view insertSubview:settings.view atIndex:0];

        [readerMain viewDidDisappear:YES];

        [settings viewDidAppear:YES];

    }
    [UIView commitAnimations];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [readerMain release];
    [settings release];
    [mSignInOutButton release];
    [mRefresh release];
    [super dealloc];
}


@end

Esto funciona como se esperaba. Después de cargar el archivo settings.nib, quiero cargar otra viewController para la configuración avanzada. Traté de hacer esto de la siguiente manera:

archivo Settings.h

//
//  Settings.h
//  iReader
//
//  Created by Vivek Manjunath on 22/08/10.
//  Copyright (c) 2010 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "ViewController.h"


@interface Settings : UIViewController {

    IBOutlet UIButton *googleReaderSettings;

}

@property (nonatomic, retain) IBOutlet UIButton *googleReaderSettings;
-(IBAction) goToViewTwo;
@end

El archivo settings.m

//
//  Settings.m
//  iReader
//
//  Created by Vivek Manjunath on 22/08/10.
//  Copyright (c) 2010 __MyCompanyName__. All rights reserved.
//

#import "Settings.h"
#import "GoogleClientLogin.h"
#import "iReaderAppDelegate.h"


@implementation Settings
@synthesize googleReaderSettings;

-(IBAction)goToViewTwo{
    GoogleClientLogin *gClient=[[GoogleClientLogin alloc]initWithNibName:@"GoogleClientLogin" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:gClient animated:YES];
    [gClient release];
}
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [googleReaderSettings release];
    [super dealloc];
}


@end

Finalmente mi archivo Delegado:

//
//  iReaderAppDelegate.h
//  iReader
//
//  Created by Vivek Manjunath on 22/08/10.
//  Copyright (c) 2010 __MyCompanyName__. All rights reserved.
//


#import <UIKit/UIKit.h>
@class ViewController;
@class Settings;

@interface iReaderAppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;
    IBOutlet ViewController *switchViewController;
    IBOutlet ViewController *settings;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet ViewController *switchViewController;
@property (nonatomic, retain) IBOutlet ViewController *settings;

@end

Y el archivo Delegate.m

//
//  iReaderAppDelegate.m
//  iReader
//
//  Created by Vivek Manjunath on 22/08/10.
//  Copyright (c) 2010 __MyCompanyName__. All rights reserved.
//


#import "iReaderAppDelegate.h"
#import "ViewController.h"

@implementation iReaderAppDelegate


@synthesize window;
@synthesize switchViewController, settings;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.
    [window addSubview:switchViewController.view];
    [window addSubview:settings.view];
    [window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillTerminate:(UIApplication *)application {

    // Save data if appropriate.
}

- (void)dealloc {

    [window release];
    [switchViewController release];
    [settings release];
    [super dealloc];
}

@end

Sin embargo, cuando se presiona el botón de cargar la vista GoogleClientLogin, no hace nada. No hay una salida de la consola tampoco. ¿Podría alguien ayudarme con este asunto? Me fijo las pérdidas de memoria y programas de NSLog que está entrando goToViewTwo.

¿Fue útil?

Solución

En la ida, veo 2 cosas en el código.

1) ¿Ha conectado el IBAction al botón, lo que debería desencadenar el cambio? (En InterfaceBuilder)

2) En su GoToViewTwo Methode, se puede asignar GClient pero nunca se suelte. Por lo que producirá una pérdida de memoria.

Mi sugerencia: Ponga una NSLog(@'Was in GoToViewTwo-Method') en su GoToViewTwo-Método de prueba si se ejecuta durante el uso de su programa. Así se puede ver si el viewcontroller es el problema o mostrando que es el problema. ; -)

Lo siento por mi Inglés. Yo sé que no es la mejor, pero he intentado escribir tan bien como pueda. ; -)

Otros consejos

Te ha configurado su navigationController en IB?

lo contrario no puedo ver un lugar donde vaya a utilizarlo en su código.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top