0

これは基本的な質問にすぎないことはわかっていますが、それでもどこかで何かが足りないので、別のクラスからtextViewにデータを渡すことで遊んでいます。このために、1つはxibファイル(ViewController)を使用し、もう1つは(secondVC)を使用しない2つのクラスを作成しました。

私がやろうとしているのは、ViewControllerクラスにtextviewがあり、secondVCからこのtextViewにデータを渡したいということです。これが私のやり方です。

//ViewController.h

#import <UIKit/UIKit.h>
#import "secondVC.h"

@interface ViewController : UIViewController{

    IBOutlet UITextView *textView;
}

@property (nonatomic, retain) UITextView *textView;

- (IBAction)go:(id)sender;

@end


//ViewController.m

- (IBAction)go:(id)sender{

    secondVC *sec = [[secondVC alloc] init];

    [sec print];

}


//secondVC.h

#import <UIKit/UIKit.h>
#import "ViewController.h"

@interface secondVC : UIViewController

- (void)print;

@end


//secondVC.m

- (void)print{

    NSString *printThis = @"This works";

    ViewController *vc = [[ViewController alloc] init];

    [vc.textView setText:printThis];

    //vc.textView.text = printThis  //Tried both
}

任意の提案をいただければ幸いです。

ありがとう

4

6 に答える 6

0

プロトコルを試してください...textView(子)から他のViewController(親)に文字列を送信する場合

于 2012-05-08T15:02:42.090 に答える
0

SecondVCから起動され、最初のメソッド(ViewController)で処理されるデリゲートメソッドが必要です。

于 2012-05-08T15:04:53.437 に答える
0

.hファイル:

#import <UIKit/UIKit.h>


@protocol StringDelegate <NSObject>

-(void)getArrayOfStrings:(NSMutableArray*)strArray;


@end



@interface WWSettings : UIViewController{


}

@property(nonatomic,assign)id<StringDelegate>delegate;

@end

.mファイル:

#import "WWSettings.h"

@implementation WWSettings
@synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)blablablaFunction{
    [delegate getArrayOfStrings:yourArray];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}
@end

あなたがそれがどのように機能するか理解していないなら..尋ねてください!私はあなたを助けるために最善を尽くします)

あなたのsecondVC

#import <UIKit/UIKit.h>
#import "WWSettings.h"
@interface secondVC : UIViewController<StringDelegate>{


    WWSettings      *obj;



}

@end

および.mファイル:

#import "secondVC.h"


@implementation secondVC

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




-(void)getArrayOfStrings:(NSMutableArray *)strArray{



    // here you get your array !!! it's a delegate function made by you in child viewController;



}

- (void)viewDidLoad
{
    obj = [[WWSettings alloc]init];
    [obj setDelegate:self];



    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
于 2012-05-08T15:10:53.190 に答える
0

ここにはいくつかの問題があります。

  1. ViewController新しいものを作成しsecondVCてメッセージを送信する必要がありますprint。それは問題ありませんが、の実装はののインスタンスを-print作成し、そのプロパティのテキストを設定しようとします。それは明らかにあなたが望むものではありません-代わりにテキストをの元のインスタンスに送り返す必要があります。ViewControllertextViewViewController

  2. の2番目のインスタンスは、アウトレットであるため、プロパティがnilに設定されViewControllerている可能性が非常に高いですが、.xibからビューをロードしていません。textViewtextView

  3. あるViewControllerが別のViewControllerのビューを台無しにするのは本当にいいことではありません。は、ビューの1つのテキストを設定しようとするのではなく、元のオブジェクトにsecondVCテキストを与える必要があります。ViewController

  4. secondVCからの通信を容易にするために、元のViewControllersecondVC追跡するプロパティを指定しますViewController。ここで行う通常のことは、のデリゲートプロトコルを定義し、secondVCそのプロトコルをで実装することですViewController。をViewController作成するsecondVCと、のデリゲートがsecondVCそれ自体に設定されます。これによりsecondVC、デリゲートへのポインタが提供されます(ViewControllerデリゲートが適切なメソッドを実装している限り、そのオブジェクトまたは他の種類のオブジェクトのどちらであるかは関係ありません)。

于 2012-05-08T15:13:54.960 に答える
0

あなたはこのようにすることができます:

//ViewController.h

#import <UIKit/UIKit.h>
#import "secondVC.h"

@interface ViewController : UIViewController{

    IBOutlet UITextView *textView;
}

@property (nonatomic, retain) UITextView *textView;

- (IBAction)go:(id)sender;

@end


//ViewController.m

- (IBAction)go:(id)sender{

    secondVC *sec = [[secondVC alloc] init];
    sec.viewController = self;
    [sec print];

}


//secondVC.h

#import <UIKit/UIKit.h>
#import "ViewController.h"

@interface secondVC : UIViewController {
ViewController *viewController;

}

@property(nonatomic, retain)ViewController *viewController;

- (void)print;

@end


//secondVC.m

@synthesize viewController;

- (void)print{

    NSString *printThis = @"This works";
    self.viewController.textView.text = printThis ; 
}
于 2012-05-08T15:17:09.763 に答える
0

最初のVC.hファイル:

#import <UIKit/UIKit.h>

@protocol textViewChildDelegate <NSObject>

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

@end
@interface textViewChild : UIViewController<UITextViewDelegate>{

    UITextView     *textView;

}
@property(nonatomic,assign)id<textViewChildDelegate>delegate;
@end

.mファイル:

#import "textViewChild.h"



    @implementation textViewChild
    @synthesize delegate;
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }



    -(void)myWorkingMethod{


        // get string from textView
        [delegate getStrings:textView.text];


    }

    - (void)viewDidLoad
    {

        textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 240, 320, 240)];

        [super viewDidLoad];
        // Do any additional setup after loading the view.
    }

次に、secondVC.hに移動します。

#import <UIKit/UIKit.h>
#import "textViewChild.h"
@interface TextViewViewController : UIViewController<textViewChildDelegate>{

    UITextView * myfirstTextView;


}

@end

および.mファイルへ:

#import "TextViewViewController.h"

@implementation TextViewViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


-(void)getStrings:(NSString *)string{


    myfirstTextView.text = string; // finally we get string from child view controller

}

- (void)viewDidUnload
{

    myfirstTextView = [[UITextView alloc]init];


    [super viewDidUnload];
    // Release any retained subviews of the main view.
}
于 2012-05-08T15:32:44.013 に答える