سؤال

أحاول تعلم Objective-C وأواجه مشكلة في الضغط على وحدة تحكم عرض جديدة عند النقر فوق زر. لديّ وحدة تحكم عرض التبديل مسؤولة عن الضغط على ملف الإعدادات. فيما يلي رمز 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

هذا يعمل كما هو متوقع. بعد تحميل ملف الإعدادات. NIB ، أريد تحميل ViewController آخر للإعدادات المتقدمة. حاولت أن أفعل هذا على النحو التالي:

SETTESS.H FILE

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

وملف المندوب

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

هل كانت مفيدة؟

المحلول

أثناء التنقل ، أرى شيئين في الكود الخاص بك.

1) هل قمت بتوصيل Ibacte بالزر ، والتي يجب أن تؤدي إلى التغيير؟ (في InterfaceBuilder)

2) في gotoviewtwo methode ، يمكنك تخصيص gclient لكنك لم تصدره أبدًا. لذلك أنت تنتج تسرب الذاكرة.

اقتراحي: ضع أ NSLog(@'Was in GoToViewTwo-Method') في GotoViewtwo-Method لاختبار ما إذا كان يتم تنفيذه أثناء استخدام البرنامج. حتى تتمكن من معرفة ما إذا كانت ViewController هي المشكلة أو إظهارها هي المشكلة. ؛-)

اسف للغتى الانجليزيه. أعلم أن هذا ليس الأفضل ، لكنني حاولت الكتابة قدر الإمكان. ؛-)

نصائح أخرى

هل قمت بإعداد NavigationController في IB؟

وإلا لا يمكنني رؤية مكان تستخدمه في الكود الخاص بك.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top