【问题标题】:Sending data from one ViewController to another.将数据从一个 ViewController 发送到另一个。
【发布时间】:2013-09-02 05:39:47
【问题描述】:

我有两个 ViewController:

1) 视图控制器

2) TestAppViewController

ViewController.h 中,我定义了一个标签,我希望从第二个viewController 发送其文本,即`TestAppViewController。

为此,我在 ViewController.h 中定义了一个@property NSString *,并在我的第二个控制器中创建了这个 ViewController 的对象。 然后将一个值传递给该属性,但 ans 仍然为零。

以下是我的代码:

@property (nonatomic, retain) NSString *lableName;*

视图控制器

#import "ViewController.h
#import "TestAppView1.h"
#import "TestAppViewController2.h"

@interface ViewController ()

@end

@implementation ViewController

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

- (void)viewWillAppear:(BOOL)animated
{ 
    [self addView1];

    NSLog(@"Label name is %@",self.lableName);
}    

- (void)didReceiveMemoryWarning
{
   [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}

- (void)addView1
{
    TestAppView1 *testAppView1Obj = [ [TestAppView1 alloc]        initWithFrame:self.view.bounds];

    [testAppView1Obj setDelegate:self];
    [testAppView1Obj setSelectorOnButtonTapped:@selector(buttonTapped)];

    [self.view addSubview:testAppView1Obj];
}

 -(void)buttonTapped
 {

    TestAppViewController2 *testAppViewController2Obj =[[TestAppViewController2    alloc]initWithNibName:@"TestAppViewController2" bundle:nil];

    [self.navigationController pushViewController:testAppViewController2Obj animated:YES];
 }

 @end

ViewController2

#import "TestAppViewController2.h"
#import "TestAppView2.h"
#import "ViewController.h"

@interface TestAppViewController2 ()

@end

@implementation TestAppViewController2

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

-(void)viewWillAppear:(BOOL)animated
{
    [self addView2];
}

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



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)addView2
{
    TestAppView2 *testAppView2Obj =[ [TestAppView2 alloc]initWithFrame:self.view.bounds];

    [testAppView2Obj setDelegate:self];
    [testAppView2Obj setSelectorOnBackButton:@selector(callbackButtonTapped)];
    [self.view addSubview:testAppView2Obj];
}

-(void)callbackButtonTapped
{

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

     viewControllerObj.lableName=@"Yogesh";    
     [self.navigationController popViewControllerAnimated:YES];


}

@end

当我尝试打印 NSLog 中的值时,它给了我 nil。

【问题讨论】:

  • 如果你也使用 arc,我建议使用 (strong, nonatomic)
  • 通过init或者使用delegate
  • 确保您已正确实现它。您正在分配一个新的视图控制器并从堆栈中弹出。代码未清除。在您的回调按钮中,分配该视图的用途是什么?使用这个。而不是弹出推送视图来查看效果。
  • @spynet..你能帮我展示一个使用委托..在控制器之间发送值的示例

标签: ios iphone objective-c callback viewcontroller


【解决方案1】:

使用委托:

在 TestAppViewController.h 中:

   @protocol messageDelegate <NSObject>
    @optional
    -(void)test:(NSString*)str;
    @end
    @interface SecondViewController : NSString
    @property (nonatomic, assign) id <messageDelegate> delegate;
    @end

在 TestAppViewController.m 中:

-(void)readyToSend
{
    [self.delegate test:@"Hello world!"];

}

在 ViewController.h 中:

@interface ViewController : UIViewController<messageDelegate>
@end

在 ViewController.m 中:在

- (void)viewDidLoad {
TestAppViewController * testAppViewController = [[TestAppViewController alloc] init];
testAppViewController.delegate = self;
}
-(void) test:(NSString*)str{
NSLog(@"%@,str");
}

希望对您有所帮助!

【讨论】:

  • 好方法.. 使用协议而不是委托者和选择器.. :) 开始使用它们。几天前。
  • 好答案。新手容易理解
猜你喜欢
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 2014-10-04
  • 1970-01-01
  • 2018-06-21
相关资源
最近更新 更多