这是我的.h文件
#import <UIKit/UIKit.h>
@interface PersonViewController : UIViewController
@property(strong,nonatomic) NSString *personTitle;这是我的.m文件
@interface PersonViewController ()
@property (weak, nonatomic) IBOutlet UILabel *titleView;
@end
@implementation PersonViewController
//stuff …
-(void)setPersonTitle:(NSString *)personTitle
{
[self.titleView setText:personTitle];// also self.titleView.text=personTitle
[self.titleView setNeedsDisplay];
NSLog(@"The title shoud match as %@ :: %@",personTitle,self.titleView.text);
}
-(NSString *)personTitle
{
return self.titleView.text;
}
//… more stuff
@end日志记录显示,该值为(null),用于self.titleView.text,而personTitle则打印适当的值。
我记得我做过很多次同样的事情,它起了作用。知道这次失败的原因吗?
更新我使用故事板来设置我的场景。我使用的是xcode-5和iOS-7。
更新:我如何调用
用户单击一个按钮,从而导致一个推送。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(@"enter prepare for segue.");
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
if ([segue.identifier isEqualToString:the_identifier_for_person]) {
NSLog(@"segue to person is progressing“);
if ([segue.destinationViewController isKindOfClass:[PersonViewController class]]) {
NSLog(@"segue to person destination is a match");
PersonViewController *aPerson = (PersonViewController *)segue.destinationViewController;
aPerson.personTitle=((MyItem*)self.allItems[indexPath.row]).title;
NSLog(@"segue to person is done");
}
}
}发布于 2014-08-01 20:47:58
您是否真的调用了-(void)setPersonTitle:(NSString *)personTitle方法?
您似乎没有正确地调用它,这将导致标题为null。
在查看了prepareForSeque之后,很明显,您没有调用该方法。实际上,您只是在更改名为@property的personTitle。
在viewDidLoad中,您应该拥有它,以便使self.titleView.text = self.personTitle;
发布于 2014-08-01 20:55:17
这听起来好像你忘了在故事板上连接你的UILabel。你能确认self.titleView不是空的吗?
发布于 2014-08-01 21:25:04
视图控制器根据需要创建视图,但只能通过调用view来识别。加载视图后,您的出口将被填充。
要么调用view强制加载,要么将字符串暂时搁置,直到得到viewDidLoad为止。
(旁白:在iOS 6之前,视图也会在低内存的情况下释放,所以惯用的做法是存储字符串并在viewDidLoad上填充)
https://stackoverflow.com/questions/25088401
复制相似问题