<iOS Xcode4 Objective-C>

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
はな

<iOS Xcode4 Objective-C>

#1

投稿記事 by はな » 13年前

<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

beatle
記事: 1281
登録日時: 14年前
住所: 埼玉
連絡を取る:

Re: <iOS Xcode4 Objective-C>

#2

投稿記事 by beatle » 13年前

まずフォーラムルールをお読みになり、codeタグの使い方を学んでください。

Objective-Cは詳しくないのですが、ぱっとみ

コード:

@class FirstViewController; 
@class SecondViewController.h;
の2行で、.hが付くか付かないかが違うのが気になるところです。

はな

Re: <iOS Xcode4 Objective-C>

#3

投稿記事 by はな » 13年前

//訂正です。

コード:


//AppDelegate.h
#import <UIKit/UIKit.h>

@class FirstViewController; 
@class SecondViewController;
@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


閉鎖

“C言語何でも質問掲示板” へ戻る