<iOS Xcode4 Objective-C>
Posted: 2012年10月18日(木) 03:04
<iOS Xcode4 Objective-C>
2つのViewを用意し遷移させるのに今までは、1stViewController, 2ndViewControllerを作り、それぞれにUIView,
UIButton(@selector(nextFirst)), ボタンメソッド「-(void)nextFirst {[self.view addSubview:second];}」 としてやってました。
今回はそれぞれのビューコントローラにはUIView, UIBUttonまでは同じく書き、-(void)nextFirst {[self.view addSubview:second];} を「AppDelegateに書いて」、ビューのボタンを押したらデリゲートのメソッドが処理してビューコントローラに返すというようにしたくていろいろ調べてみたら、継承?プロトコル?シーンの管理?とかが怪しいのかな?とその辺をよんでデリゲートではないのですが「他のビュークラスにやらせる」っていうサンプルがあったのでそれを手本にやって見たんですがエラーは無いものの動きません。改善を教えて下さい。以下が書いたコードです。
----------------------------------------------------------------------------------------------------------------------------------
AppDelegate.h
#import <UIKit/UIKit.h>
@class FirstViewController;
@class SecondViewController.h;
@interface Method_CodeAppDelegate : UIResponder <UIApplicationDelegate>
{
FirstViewController *firstView;
SecondViewController *secondView;
}
@property (strong, nonatomic) UIWindow *window;
-(void)toSecond;
@end
---------------------------------------------------------------------------------------------------------------------
AppDelegate.m
#import "Method_CodeAppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
@implementation Method_CodeAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor blueColor];
firstView = [[FirstViewController alloc] init];
secondView = [[SecondViewController alloc]init];
self.window.rootViewController = firstView;
[_window addSubview:secondView.view];
[self.window makeKeyAndVisible];
return YES;
}
-(void)toSecond
{
[_window addSubview:secondView.view];
}
@end
-----------------------------------------------------------------------------------------------------------------------------------------
FirstViewController.h
#import <UIKit/UIKit.h>
@protocol FirstDelegate;
@interface FirstViewController : UIViewController
{
UIView *firstView;
id <FirstDelegate> delegate;
}
//@property(retain, nonatomic)id <FirstDelegate> delegate;
@end
@protocol FirstDelegate
-(void)toSecond;
@end
----------------------------------------------------------------------------------------------------------------------------------------
FirstViewController.m
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
//@synthesize delegate;
- (void)viewDidLoad
{
[super viewDidLoad];
firstView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
firstView.backgroundColor = [UIColor blueColor];
[self.view addSubview:firstView];
UIButton *firstBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
firstBtn.frame = CGRectMake(130, 300, 60, 50);
firstBtn.tintColor = [UIColor blueColor];
firstBtn.showsTouchWhenHighlighted = YES;
[firstBtn addTarget:self action:@selector(toDelegate) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:firstBtn];
}
-(void)toDelegate
{
[delegate toSecond];
}
@end
-----------------------------------------------------------------------------------------------------------
SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
{
UIView *secondView;
}
@end
--------------------------------------------------------------------------------------------------------------------
SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
secondView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
secondView.backgroundColor = [UIColor redColor];
[self.view addSubview:secondView];
}
@end
2つのViewを用意し遷移させるのに今までは、1stViewController, 2ndViewControllerを作り、それぞれにUIView,
UIButton(@selector(nextFirst)), ボタンメソッド「-(void)nextFirst {[self.view addSubview:second];}」 としてやってました。
今回はそれぞれのビューコントローラにはUIView, UIBUttonまでは同じく書き、-(void)nextFirst {[self.view addSubview:second];} を「AppDelegateに書いて」、ビューのボタンを押したらデリゲートのメソッドが処理してビューコントローラに返すというようにしたくていろいろ調べてみたら、継承?プロトコル?シーンの管理?とかが怪しいのかな?とその辺をよんでデリゲートではないのですが「他のビュークラスにやらせる」っていうサンプルがあったのでそれを手本にやって見たんですがエラーは無いものの動きません。改善を教えて下さい。以下が書いたコードです。
----------------------------------------------------------------------------------------------------------------------------------
AppDelegate.h
#import <UIKit/UIKit.h>
@class FirstViewController;
@class SecondViewController.h;
@interface Method_CodeAppDelegate : UIResponder <UIApplicationDelegate>
{
FirstViewController *firstView;
SecondViewController *secondView;
}
@property (strong, nonatomic) UIWindow *window;
-(void)toSecond;
@end
---------------------------------------------------------------------------------------------------------------------
AppDelegate.m
#import "Method_CodeAppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
@implementation Method_CodeAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor blueColor];
firstView = [[FirstViewController alloc] init];
secondView = [[SecondViewController alloc]init];
self.window.rootViewController = firstView;
[_window addSubview:secondView.view];
[self.window makeKeyAndVisible];
return YES;
}
-(void)toSecond
{
[_window addSubview:secondView.view];
}
@end
-----------------------------------------------------------------------------------------------------------------------------------------
FirstViewController.h
#import <UIKit/UIKit.h>
@protocol FirstDelegate;
@interface FirstViewController : UIViewController
{
UIView *firstView;
id <FirstDelegate> delegate;
}
//@property(retain, nonatomic)id <FirstDelegate> delegate;
@end
@protocol FirstDelegate
-(void)toSecond;
@end
----------------------------------------------------------------------------------------------------------------------------------------
FirstViewController.m
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
//@synthesize delegate;
- (void)viewDidLoad
{
[super viewDidLoad];
firstView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
firstView.backgroundColor = [UIColor blueColor];
[self.view addSubview:firstView];
UIButton *firstBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
firstBtn.frame = CGRectMake(130, 300, 60, 50);
firstBtn.tintColor = [UIColor blueColor];
firstBtn.showsTouchWhenHighlighted = YES;
[firstBtn addTarget:self action:@selector(toDelegate) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:firstBtn];
}
-(void)toDelegate
{
[delegate toSecond];
}
@end
-----------------------------------------------------------------------------------------------------------
SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
{
UIView *secondView;
}
@end
--------------------------------------------------------------------------------------------------------------------
SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
secondView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
secondView.backgroundColor = [UIColor redColor];
[self.view addSubview:secondView];
}
@end