0

在我的 iPhone 应用程序中,我有ViewController1一个UILabel被调用的应用程序selectedBook,我在情节提要中添加了它的视图并连接了插座。

我有另一个视图控制器,我从toViewController2传递一些数据,其中的文本就是其中之一。ViewController2ViewController1UILabel

ViewController1我写了以下代码:

在 ViewController1.h

@interface ViewController1 : UIViewController{

  IBOutlet UILabel *selectedBook;

  NSString *BookName;
  NSString *BookCover;
  NSString *BookAbout;
}

@property (nonatomic, retain) NSString *BookName;
@property (nonatomic, retain) NSString *BookCover;
@property (nonatomic, retain) NSString *BookAbout;

-(void) setTheSelectedBook:(NSString *)bookName bookCover:(NSString *)bookCover bookAbout:(NSString *)bookAbout userID:(int)userID;

在 ViewController1.m

-(void) setTheSelectedBook:(NSString *)bookName bookCover:(NSString *)bookCover bookAbout:(NSString *)bookAbout userID:(int)userID
{
    BookName = bookName;
    BookCover = bookCover;
    BookAbout = bookAbout;

    NSLog(@"in setTheSelectedBook, BookName: %@ - BookCover: %@ - BookAbout: %@ - userID: %d", BookName, BookCover, BookAbout, userID);

    selectedBook.text = bookName;
}

viewController2我写了以下代码:

ViewController1 *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Suggest"];
[viewController setTheSelectedBook:@"MybookName" bookCover:@"MyBookCover" bookAbout:@"MuBookAbout" userID:1];

我的数据正确传递,我在输出中看到它,但问题是UILabel文本没有更新,这意味着这一行

selectedBook.text = bookName;

不起作用。

我试着把它写成这样selectedBook.text = @"someText";,这很有效。

我无法理解selectedBook.text = bookName;工作缺少什么?我希望有人在这个问题上帮助我。

提前致谢。

4

2 回答 2

0

我在这里看到的唯一解释是, bookName 不是 NSString。

将以下行添加到该方法:

NSLog(@"bookName class is %@", [bookName class]);
NSLog(@"selectedBook class is %@", [selectedBook class]);

XCode 在运行时将什么写入日志?

于 2013-11-14T07:00:12.173 回答
0

你的代码有点混乱。切勿以大写字母开头变量。BookName应该是bookName_bookName。我认为这是问题之一。

如果您改为使用变量创建 Book-class:

@property (copy) NSString *name;
@property (copy) NSString *cover;
@property (copy) NSString *about;

然后在您viewController1通过实例化您的 Book-class 并设置它的变量来创建这本书时,您可以将您的 Book-object 发送到您的viewController2.

这对你来说可能会更容易。

如果您只想更改自己的代码,可以尝试:

在您的 h 文件中:

@property (copy) NSString *BookName;
@property (copy) NSString *BookCover;
@property (copy) NSString *BookAbout;

在你的 m 文件中:

-(void) setTheSelectedBook:(NSString *)bookName bookCover:(NSString *)bookCover bookAbout:(NSString *)bookAbout userID:(int)userID
{
    _BookName = bookName;
    _BookCover = bookCover;
    _BookAbout = bookAbout;

    NSLog(@"in setTheSelectedBook, BookName: %@ - BookCover: %@ - BookAbout: %@ - userID: %d", BookName, BookCover, BookAbout, userID);

    selectedBook.text = _BookName;
}
于 2013-11-13T10:11:23.700 回答