2014-09-18 70 views
0

我有两个viewControllers其中一个包含UILabel和UIButton,另一个是UITableView。当按下第一个视图中的UIButton时,将显示新的ViewController,并且当选择tableview中的一行时,tableview将被取消,并且第一个viewController中的UILabel应该更新,以便其文本与点击的单元名称相同实现代码如下。 代码:UILabel没有添加到视图

在第一个的viewController:

- (void)changeTitleWithString:(NSString *)title 
{ 
    UILabel* selected_station = [[UILabel alloc ]initWithFrame:CGRectMake(45, 153+45, 231, 20)]; 
    [selected_station setFont:[UIFont systemFontOfSize:16.0]]; 
    selected_station.textAlignment = NSTextAlignmentCenter; 
    selected_station.text = [NSString stringWithFormat:@"%@", title]; 
    [self.view addSubview:selected_station]; 
    NSLog(@"%@", selected_station); 
    NSLog(@"%@", title); 
} 

在第二的viewController

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    self.currentString = [NSString stringWithFormat:@"Cell %d", indexPath.row+1]; 

    NewViewController *viewCOntroller = [[NewViewController alloc] init]; 
    [viewController changeTitleWithString:self.currentString]; 

    [self.navigationController dismissViewControllerAnimated:YES completion:nil];  
} 

的changeTitleWithString函数被调用,并从实现代码如下字符串中的功能被成功接收,但的UILabel赢没有出现。当我注销UILabel(selected_station)时,它不是零,其文本是正确的。为什么字符串不显示在视图上?

+0

请您可以共享的完整代码,如何在第一页视图控制器礼物按钮点击第二视图控制器。 – Kaps18 2014-09-18 17:28:58

+1

你正在创建一个新的'NewViewController'实例,但你没有显示它,所以它基本上意味着你什么都不做。您需要对第一个视图控制器的引用,对第二个视图控制器传递的对'changeTitleWithString'方法或类似的引用。 – trydis 2014-09-18 17:30:40

+0

你应该写这个评论为答案@trydis – 2014-09-18 17:39:51

回答

1

这看起来不像您正在引用回以前分配的视图控制器(NewViewController)。而是你正在分配一个新的NewViewController。

一个更好的办法是创建第二个视图控制器的委托协议,使NewViewController它的委托 -

因此,创建具有委托性质的第二视图控制器。

像这样

@protocol SecondViewControllerDelegate; 

@interface SecondViewController : UIViewController 

@property (nonatomic, strong) NSString *currentString; 
@property (nonatomic, assign) id<SecondViewControllerDelegate>delegate; 
@end 

//---------------------------------------------------------------------------------- 
@protocol SecondViewControllerDelegate <NSObject> 

@optional 
-(void) secondViewController:(SecondViewController*)secondViewController didChangeSelectionText:(NSString *)string; 
@end 

现在在SecondViewController的实施做这样的事

@implementation SecondViewController 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    self.currentString = [NSString stringWithFormat:@"Cell %ld", indexPath.row+1]; 

    if ([self.delegate respondsToSelector:@selector(secondViewController:didChangeSelectionText:)]) { 
     [self.delegate secondViewController:self didChangeSelectionText:self.currentString]; 
    } 
    [self.navigationController dismissViewControllerAnimated:YES completion:nil]; 
} 

@end 

现在在FirstViewController(您NewViewController)的执行做这样的事

@implementation FirstViewController 

-(void) showSecondController 
{ 
    SecondViewController *viewController = [[SecondViewController alloc] init]; 
    [viewController setDelegate:self]; // set this controller as the delegate 
    // add the rest of the display code here 
} 

#pragma mark - SecondViewControllerDelegate Methods 
-(void) secondViewController:(SecondViewController*)secondViewController didChangeSelectionText:(NSString *)string 
{ 
    //change your text here 
} 
@end 

这应该足以让你pointe d在正确的方向

+0

感谢您的答案!我不知道我是否理解你。方法声明应该放在第二个viewController或NewViewController中的.h文件中?方法实现应该放在哪里? – 2014-09-18 18:43:14

+0

听起来像代表的想法你有点困惑。看看https://developer.apple.com/library/ios/documentation/cocoa/conceptual/programmingwithjectivec/WorkingwithProtocols/WorkingwithProtocols。html – pds 2014-09-18 21:54:14

+0

我用更完整的代码示例更新了答案 – pds 2014-09-18 22:26:39

1

在FirstViewController

- (void)changeTitleWithString:(NSString *)title :(UIView *)labelView 
{ 
    UILabel* selected_station = [[UILabel alloc ]initWithFrame:CGRectMake(45, 153+45, 231, 20)]; 
    [selected_station setFont:[UIFont systemFontOfSize:16.0]]; 
    selected_station.textAlignment = NSTextAlignmentCenter; 
    selected_station.text = title; 
    [labelView addSubview:selected_station]; 
    NSLog(@"%@", selected_station); 
    NSLog(@"%@", title); 
} 

InSecondViewController

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    self.currentString = [NSString stringWithFormat:@"Cell %d", indexPath.row+1]; 

    NewViewController *viewCOntroller = [[NewViewController alloc] init]; 
    [viewController changeTitleWithString:self.currentString:self.view]; 

    [self.navigationController dismissViewControllerAnimated:YES completion:nil];  
} 

我希望它能帮助