Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UILabel是';t添加到视图中_Ios_Objective C_Iphone_Uitableview - Fatal编程技术网

Ios UILabel是';t添加到视图中

Ios UILabel是';t添加到视图中,ios,objective-c,iphone,uitableview,Ios,Objective C,Iphone,Uitableview,我有两个ViewController,其中一个包含UILabel和UIButton,另一个包含UITableView。当按下第一个视图中的UI按钮时,将显示新的ViewController,当选择tableview中的一行时,tableview将被取消,并且应更新第一个ViewController中的UILabel,使其文本与tableview中点击的单元格名称相同。 代码: 在第一个Viewcontroller中: - (void)changeTitleWithString:(NSString

我有两个ViewController,其中一个包含UILabel和UIButton,另一个包含UITableView。当按下第一个视图中的UI按钮时,将显示新的ViewController,当选择tableview中的一行时,tableview将被取消,并且应更新第一个ViewController中的UILabel,使其文本与tableview中点击的单元格名称相同。 代码:

在第一个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);
}
在第二视图控制器中

- (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];    
}
- (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);
}
- (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];    
}

调用changeTitleWithString函数,并在函数中成功接收tableview中的字符串,但UILabel不会显示。当我注销UILabel(选定的_站)时,它不是nil,并且它的文本是正确的。为什么该字符串没有显示在视图上?

这看起来不像是引用回以前分配的视图控制器(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
@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
@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
现在在FirstViewController(您的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
@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
@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
这应该足以让您在FirstViewController中指向正确的方向

- (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];    
}
- (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);
}
- (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];    
}
UnsecondViewController

- (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];    
}
- (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);
}
- (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];    
}

我希望它能有所帮助

请分享完整的代码,第一视图控制器中的单击按钮如何显示第二视图控制器。您正在创建一个新的“NewViewController”实例,但您没有显示它,因此这基本上意味着您什么也没做。您需要对传递给第二视图控制器的第1视图控制器、对“changeTitleWithString”方法或类似方法的引用。您应该将此注释作为answer@TryDistanks来编写,以获取答案!我不确定我是否理解你的意思。方法声明应该放在第二个viewController或NewViewController的.h文件中吗?那么方法的实现应该去哪里呢?听起来委托的想法让你有点困惑。看一看我用更完整的代码更新了答案谢谢你的回答!