0

我知道这只是一个基本问题,但我仍然在某个地方遗漏了一些东西,我正在将数据从另一个类传递给 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
}

任何建议,将不胜感激。

谢谢

4

6 回答 6

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 回答
0

尝试使用协议...如果您想将字符串从 textView(child) 发送到其他 ViewController(parent)

于 2012-05-08T15:02:42.090 回答
0

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

于 2012-05-08T15:04:53.437 回答
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

.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.
}
于 2012-05-08T15:10:53.190 回答
0

这里有几个问题:

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

  2. 第二个实例的属性ViewController很可能textView设置为 nil,因为textView它是一个出口,但您还没有从 .xib 加载它的视图。

  3. 一个视图控制器弄乱另一个视图控制器的视图真的不好。应该将secondVC文本提供给原始ViewController对象,而不是尝试设置其视图之一的文本。

  4. 为方便来往secondVCViewControllersecondVC个物业原作跟踪ViewController。这里通常要做的事情是定义一个委托协议secondVC并在ViewController. 创建时,它将委托设置ViewController为自身。这给出了一个指向它的委托的指针(它不应该关心它是一个对象还是其他类型的对象,只要委托实现了正确的方法)。secondVCsecondVCsecondVCViewController

于 2012-05-08T15:13:54.960 回答