-1

可能重复:
将模型对象从一个视图控制器传递到导航堆栈中的另一个

我正在尝试在 iphone 中的两个视图之间传递控件。我已经尝试过了,当您从第一个视图控制器传递到第二个时它工作正常,但是当我单击第二个时,它会空白。为什么呢?任何帮助表示赞赏.. 谢谢...

视图控制器.h

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

@interface ViewController : UIViewController <SecondViewControllerDelegate>
{
    IBOutlet UILabel *lbl;

}
-(IBAction)passdata:(id)sender;

@end

视图控制器.m

#import "ViewController.h"
#import "ViewController2nd.h"

@interface ViewController ()

@end

@implementation ViewController

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void) changeLabel:(NSString*)str{
    lbl.text = @"Hello";
}

-(IBAction)passdata:(id)sender{
    ViewController2nd *second = [[ViewController2nd alloc] initWithNibName:nil bundle:nil];
    [self presentViewController:second animated:YES completion:^{ }];
}

@end

ViewController2nd.h

#import <UIKit/UIKit.h>


@protocol SecondViewControllerDelegate <NSObject>
@optional
-(void) changeLabel:(NSString*)str;
@end

@interface ViewController2nd : UIViewController{

    IBOutlet UIButton *bttn;

}

-(IBAction)bttnclicked;
-(IBAction)back:(id)sender;
@end

ViewController2nd.m

#import "ViewController2nd.h"
#import "ViewController.h"

@interface ViewController2nd ()

@end

@implementation ViewController2nd

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

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

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

-(IBAction)bttnclicked{

}

-(IBAction)back:(id)sender{
    ViewController *first = [[ViewController alloc] initWithNibName:nil bundle:nil];
    [self presentmodalViewController:first animated:YES completion:^{ }];
}

@end

我错过了什么吗?

4

1 回答 1

1

如果您的要求是转到第一个视图,请更改back:方法,例如:

-(IBAction)back:(id)sender
 {
     [self dismissViewControllerAnimated:YES completion:NULL];
 }

不要将 parentView 显示为 chilkdViewController 的 childView。它还会产生内存问题和逻辑问题。

因此,如果您想从 childView 转到 parentView,请关闭 childView,并且永远不要创建父对象并将其呈现在那里。

于 2012-10-23T07:51:02.820 回答