将数据从另一个类传递给textView

时间:2012-05-08 14:56:45

标签: objective-c macos

我知道这只是一个基本问题,但仍然在某些地方我遗漏了一些东西,我正在玩将数据传递给另一个类的textView。为此我创建了两个类,一个是xib文件(ViewController),另一个是没有(secondVC)。

我想要做的是我在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
}

任何建议都将不胜感激。

由于

6 个答案:

答案 0 :(得分:0)

尝试使用协议...如果要将字符串从textView(子)发送到其他ViewController(父级)

答案 1 :(得分:0)

您需要一个从SecondVC触发并在第一个处理的委托方法(ViewController)。

答案 2 :(得分: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

如果你不明白它是如何工作的......请问!我会尽力帮助你)

你的第二个VC

#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.
}

答案 3 :(得分:0)

这里有一些问题:

  1. 您已经ViewController创建了新的secondVC并向其发送了print条消息。这没关系,但是-print的实现会创建一个不同的 ViewController实例,并尝试设置它的textView属性的文本。这显然不是您想要的 - 您应该将文本发送回ViewController的原始实例。

  2. ViewController的第二个实例非常可能将textView属性设置为nil,因为textView是一个插座,但您尚未从.xib加载其视图

  3. 对于一个视图控制器而言,确实并不好用于混淆另一个视图控制器的视图。 secondVC应该将文本提供给原始ViewController对象,而不是尝试设置其中一个视图的文本。

  4. 为了促进从secondVCViewController的沟通,请为secondVC提供一个属性,以跟踪原始ViewController。这里通常要做的是为secondVC定义委托协议,并在ViewController中实现该协议。 ViewController创建secondVC时,会将secondVC的委托设置为自己。这为secondVC提供了一个指向其委托的指针(它不应该关心它是ViewController还是其他类型的对象,只要委托实现正确的方法。)

答案 4 :(得分: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 ; 
}

答案 5 :(得分: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.
}