PushViewController不起作用
-
30-09-2019 - |
题
我正在尝试学习Objective-C,并在单击按钮时遇到了一个问题。我有一个开关视图控制器,当我按“设置工具栏”按钮时,负责按下settings.nib文件。这是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
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
这是按预期工作的。加载settings.nib文件后,我想为高级设置加载另一个ViewController。我试图这样做以下操作:
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
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
最后我的委托文件:
//
// 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
和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
但是,当我按按钮加载GoogleClientLogin视图时,它无能为力。也没有控制台输出。有人可以帮我解决这个问题吗?我修复了内存泄漏,NSLOG表明它正在输入gotoviewtwo。
解决方案
在旅途中,我在您的代码中看到了2件事。
1)您是否将iBaction连接到按钮,它应该触发更改? (在接口构成中)
2)在您的gotoviewtwo methode中,您可以分配gclient,但您永远不会发布它。因此,您会产生内存泄漏。
我的建议:放一个 NSLog(@'Was in GoToViewTwo-Method')
在您的gotoviewtwo方法中,用于测试使用程序时是否执行。因此,您可以查看ViewController是否是问题或显示它是问题所在。 ;-)
对不起我的英语不好。我知道这不是最好的,但是我尽力写作。 ;-)
其他提示
您是否在IB中设置了NavigationController?
否则,我看不到您在代码中使用它的地方。
不隶属于 StackOverflow