将NSString从一个viewController传递给另一个没有IBAction的另一个

时间:2014-04-09 08:00:56

标签: ios objective-c uiviewcontroller parameter-passing

我正在尝试将NSString变量从一个视图控制器传递到另一个视图控制器,但我只能通过使用按钮IBAction找到有关这样做的教程和信息,但是无法成功传递它。没有使用按钮,有没有简单的替代方案?

#import "DetailController.h"
#import "City.h"
#import "VideoController.h"

@interface DetailController ()

@end

@implementation DetailController
@synthesize city, ClubName, Price, Vip, Promo, remain,p,deal,money,camera,cam;


- (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.
    UIFont *labelFont=[UIFont fontWithName:@"Deutsch Gothic" size:20.0];
    UIFont *myFont=[UIFont fontWithName:@"Deutsch Gothic" size:30.0];
    UIFont *titleFont=[UIFont fontWithName:@"Deutsch Gothic" size:40.0];
    NSString * name= self.city.clubName;
    NSString * line= self.city.clubLine;
    NSString * description= self.city.promo;
    NSString * price= self.city.price;
    cam=self.city.camera;
    remain.font=labelFont;
    remain.text=@"VIP Remaining :";
    p.font=labelFont;
    p.text=@"Price :";
    money.font=myFont;

    deal.font=labelFont;
    deal.text=@"Promotions :";

    ClubName.font=titleFont;
    ClubName.text=name;
    Vip.font=myFont;
    Vip.text=line;
    Price.font=myFont;
    Price.text=price;
    Promo.font=labelFont;
    Promo.text=description;

}

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

@end

我正在尝试将cam或相机传递给videoController

1 个答案:

答案 0 :(得分:2)

如果您在视图控制器之间尝试Push,则可以在视图控制器中创建属性,然后在按下视图控制器之前设置该值。您可以通过以下方式执行此操作: -

在View Controller2中声明属性

@property(nonatomic,strong) NSString *passedString;
View Controller 1中的

执行此操作

ViewController2 *vc2 = [ViewController2 alloc]initWithNibName:@"ViewController2" bundle:nil];
[vc setPassedString:@"Hello"];
[self.navigationController pushViewController:vc2 animated:YES];