我有两个viewControllers,其中一个包含UILabel和一个UIButton,另一个包含UITableView。当按下第一个视图中的UIButton时,将显示新的ViewController,当选择tableview中的一行时,将取消tableview,并且应更新第一个viewController中的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函数并在函数中成功接收来自tableview的字符串,但UILabel不会显示。当我注销UILabel(selected_station)时,它不是零并且其文本是正确的。为什么字符串不显示在视图上?
答案 0 :(得分: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
这足以让你指向正确的方向
答案 1 :(得分: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];
}
我希望它有所帮助