2009-07-17 4 views
0

私はFirstViewとSecondという名前の2つのUIViewcontrollerクラスを持っています... firstviewボタンをクリックすると、secondviewに1つの文字列を渡し、その文字列をsecondview uitextviewに表示します...しかし、私は、コードを掲載UITextViewアップデート

のTextView ... ....私はここに

をしないのです何

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


@interface FirstViewController : UIViewController { 
    IBOutlet UIButton *btnView; 
} 

@property(nonatomic,retain) UIButton *btnView; 

-(IBAction)UpdateSecondView:(id)sender; 


@end 

//first.m 
#import "FirstViewController.h" 
#import "SecondViewController.h" 
#import "testtabbarAppDelegate.h" 


@implementation FirstViewController 
@synthesize btnView; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 
     // Initialization code 
    } 
    return self; 
} 

/* 
Implement loadView if you want to create a view hierarchy programmatically 
- (void)loadView { 
} 
*/ 


- (void)viewDidLoad { 

} 

-(IBAction)UpdateSecondView:(id)sender 
{ 
    SecondViewController *object=[[SecondViewController alloc]initWithNibName:@"SecondView" bundle:nil]; 
    [object UpdateText:@"Testing"]; 
    [object release]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview 
    // Release anything that's not essential, such as cached data 
} 


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


@end 

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


@interface SecondViewController : UIViewController { 
    IBOutlet UITextView *textView; 
    NSString *globalString; 
} 
@property(nonatomic,copy) NSString *globalString; 
@property(nonatomic,retain) UITextView *textView; 

-(void)UpdateText:(NSString *)string; 

@end 
//second.m 
// 
// SecondViewController.m 
// testtabbar 
// 
// Created by mac user on 6/9/09. 
// Copyright 2009 __MyCompanyName__. All rights reserved. 
// 

#import "SecondViewController.h" 


@implementation SecondViewController 
@synthesize textView; 
@synthesize globalString; 


//NSString *[email protected]""; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 
     // Initialization code 

    } 
    return self; 
} 

/* 
Implement loadView if you want to create a view hierarchy programmatically 
- (void)loadView { 
} 
*/ 

- (void)viewDidLoad 
{ 

} 

-(void)UpdateText:(NSString *)string 
{ 
    textView.text=string; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
} 

- (BOOL)textViewShouldReturn:(UITextView *)theTextView { 
    // When the user presses return, take focus away from the text field so that the keyboard is dismissed. 
    if (theTextView == textView) { 
     [textView resignFirstResponder]; 
    } 
    return YES; 
} 


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview 
    // Release anything that's not essential, such as cached data 
} 


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


@end 

は誰も私を助けることができますか?

事前のおかげで...

答えて

1

私はこれらの両方のニブとして実装され、UITabBarControllerにされているという仮定を作るつもりです。

私はあなたがクラスとインスタンスのコンセプトを混同していると思います。クラスは、コードと状態の説明です(大まかに言えば)。その説明を使用して、そのクラスのインスタンスを作成できます(インスタンス化と呼ばれます)。そのクラスのすべてのインスタンスは一意であり、別々です。あるインスタンスを変更すると、他のインスタンスには影響しません。

さて、なぜ私はこれに入るのですか? FirstViewControllerとSecondViewControllerはインスタンスではなくクラスです。あなたが持っているnibファイルには、それらのコントローラの特定のインスタンスの設定が記述されており、ロードされると、それらの設定でコントローラがインスタンス化されます。 UpdateSecondViewで何をしているのかは、別のSecondViewControllerを作成し、その中に値を設定してすぐに破棄します。あなたがする必要があるのは、NIBがロードされたときに構築されているSecondViewControllerのインスタンスの値を設定することです。

実際にあなたのペン先を見ずに、レイアウトがどのように配置されているか正確には説明できませんが、基本的な要点は、FirstViewControllerでインスタンス変数を作成し、 (secondViewControllerが設定されませんので)あなたはちょうどそれが動作しません上記の変更を実行すると今

//Updated @interface 
@interface FirstViewController : UIViewController { 
    IBOutlet UIButton *btnView; 
    IBOutlet SecondViewController *secondViewController; 
} 

@property (nonatomic, retain) SecondViewController *secondViewController; 
@end 

//Updated @implementation 
-(IBAction)UpdateSecondView:(id)sender 
{ 
    [self.secondViewController UpdateText:@"Testing"]; 
} 

:SecondViewControllerの適切なインスタンスまで、このようSecondViewControllerのインスタンスを変更するためにそれを使用より前に言ったように、両方のインスタンスの知識を持ったコードをいくつか用意する必要があります。新しいIBOuletへのリンクをドラッグするか、別の場所でプログラムで実行することで、両方を実装しているUITabBarControllerのペン先でペン先を行うことができます。