ios推送控制器视图以编程方式没有故事板或xib

时间:2014-04-01 19:33:39

标签: ios iphone objective-c xcode

在ViewControllerA中,我有一个包含三行数据的表(三条消息)。单击一行,我正在尝试使用其中一行(名称)中的文本推送到ViewControllerB。我遇到的问题是我正在尝试以编程方式执行所有操作,而不是使用故事板ID或故事板segues。这可能吗?这就是我所拥有的。

ViewControllerA m。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  Messages *cell = (Messages *)[tableView cellForRowAtIndexPath:indexPath];
  NSString *from = [NSString stringWithFormat:@"%@", cell.from.text];
  ViewControllerB *VCB = [[ViewControllerB alloc]init];
  VCB.from = from;
  [self.navigationController pushViewController:VCB animated:YES];
  //using this^ creates a new view controller with a black screen but passes the variable
  //[self performSegueWithIdentifier:@"pushMessage" sender:self];
  using this^ shows my ViewControllerB perfectly but the value isn't set
}

ViewControllerB h。

@property (strong, nonatomic) NSString *from;

ViewControllerB m。

-(void)viewDidLoad{
    NSLog(@"%@", from);
}

有没有办法在VCA中启动新的viewcontrollerB并获得正确的推送动画?

5 个答案:

答案 0 :(得分:2)

我相信我明白你在问什么,而你在这里几乎走在正确的轨道上。您说它在您使用[self performSegueWithIdentifier:@"pushMessage" sender:self];时有效,但未设置该值。

因此,在标题中声明NSString“from”并在之前的代码中删除[self.navigationController pushViewController:CVC animated:YES];

//ViewControllerA.h

@property (copy, nonatomic) NSString *from;



//ViewControllerA.m

@synthesize from;

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  Messages *cell = (Messages *)[tableView cellForRowAtIndexPath:indexPath];
  from = [NSString stringWithFormat:@"%@", cell.from.text];

  [self performSegueWithIdentifier:@"pushMessage" sender:self];
}

然后您需要使用prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

 //ViewControllerA.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{

    if ([[segue identifier] isEqualToString:@"pushMessage"]) {

        ViewControllerB *vcb = [segue destinationViewController];

        [vcb setFrom:[self from]];
    }

}

编辑

嗯,我觉得这是一个非常错误的方法。但是,这将满足您在问题中提出的所有条件。我已经检查过,这确实有效:

首先,转到故事板,单击View Controller B,单击身份检查器并更改Storyboard ID。同时单击“使用故事板ID”的检查。

Identity Inspector

在ViewControllerA中:

//ViewControllerA.h

- (void)callViewControllerWithCellString:(NSString *)str;

//ViewControllerA.m

- (void)callViewControllerWithCellString:(NSString *)str
{
    NSString * storyboardName = @"Main";

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    SecondViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewControllerB"];
    [vc setFrom:str];

    [[self navigationController] pushViewController:vc animated:YES];

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Messages *cell = (Messages *)[tableView cellForRowAtIndexPath:indexPath];
    NSString *from = [NSString stringWithFormat:@"%@", cell.from.text];

    [self callViewControllerWithCellString:from];
}

答案 1 :(得分:1)

所以,让我们看看我是否明白这一点。

  1. 您希望将所单击行中的文本传递给您要呈现的视图控制器
  2. 您希望在不使用故事板的情况下执行此操作
  3. 听起来很简单。看看这个。

    // I actually recommend this option
    // Simply initialize your view controller with the text that you want to pass
    ViewControllerB * VCB = [[ViewControllerB alloc] initWithText:from];
    
    //  For this to work, you'll have to declare a new init method in your .h file like this
    -(void)initWithText:(NSSTring *)text;
    
    // You should also declare an iVar to store your text
    NSString * fromText;
    
    // and implement it in your .m file like this
    -(void)initWithText:(NSString *)text
    {
        self = [super init];
        if (self)
        {
            fromText = text;
        }
        return self;
    }
    

    // Or, you could do this. 
    // In ViewControllerB.h, create a property
    @property (nonatomic, strong) NSString * fromText;
    
    // In ViewControllerB.m, synthesize it
    @synthesize fromText;
    
    // Then you can pass that property a value like this
    ViewControllerB * VCB = [[ViewControllerB alloc] init];
    VCB.fromText = from;
    

答案 2 :(得分:1)

变量应该是小写的。

应该替换您的ViewControllerB。你是否以编程方式创建所有视图元素?如果你没有,那么你最好从XIB或故事板加载它。你可以做到这一点而不必去做。

但我想覆盖perpareForSegue:方法并在那里设置from属性要容易得多。这就是perpareForSegue的用途,当你需要以编程方式进行一些自定义,同时使用故事板来处理所有简单的事情。

答案 3 :(得分:1)

首先,您应该检查您的代码。您正在将名为CVC的viewController推送到导航控制器上。我认为这是一个错字。

你是否覆盖了ViewControllerB" initWithNibName:bundle:"因为我看到你正在调用init。如果你没有,ViewController将如何知道要加载的nib?

此外,建议您在viewDidLoad调用中调用[super viewDidLoad]。

答案 4 :(得分:1)

您也可以通过编程方式推送视图。 你似乎做了以下

ViewControllerB *VCB = [[ViewControllerB alloc]init];
VCB.from = from;
[self.navigationController pushViewController:CVC animated:YES];

这意味着您想要推送VCB而不是CVC。此更改应解决您的问题。 您还可以在viewDidLoad中放置一个断点来验证您的新ViewController是否已加载。

如果您也在使用VCB的故事板,我认为您应该遵循 -

ViewControllerB *VCB = [self.storyboard instantiateViewControllerWithIdentifier:@"VCBIdentifier"];

其中VCBIdentifier是Identity Inspector中标记的ViewControllerB的标识符 - > StoryBoardID。 我认为,使用当前的init,它创建了一个空白/空的默认viewcontroller。

相关问题