在iphone中传递两个视图之间的控制

时间:2012-10-23 07:42:06

标签: iphone ios ios6

  

可能重复:
  Passing model objects from one view controller to another in a navigation stack

我试图在iphone中的两个视图之间传递控件。我试过这个,当你从第一个视图控制器传递到第二个视图控制器时它工作正常,但是当我点击第二个视图控制器时,它会消隐。为什么会这样?任何帮助表示赞赏..谢谢...

Viewcontroller.h

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

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

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

@end

Viewcontroller.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

我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

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

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

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

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