简体   繁体   中英

Can't update UIlabel text

In my iPhone App I have ViewController1 that contains a UILabel called selectedBook and I added to its view in the storyboard and I connected the outlets.

I have another view controller called ViewController2 and I pass some data from ViewController2 to ViewController1 and the text of the UILabel is one of them.

In ViewController1 I wrote the following code:

in 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;

In 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;
}

In viewController2 I wrote the following code:

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

My data pass properly and I see it in the output, but the problem is that the UILabel text is not updated, meaning that this line

selectedBook.text = bookName;

does not work.

I tried writing it as selectedBook.text = @"someText"; and this works.

I can't understand what's missing to make selectedBook.text = bookName; work? I hope someone help me in this issue.

Thanks in advance.

Your code is a bit confusing. Never begin your variables with a capital letter. BookName should be bookName or _bookName . I think that's one of the problems.

If you instead create a Book-class with the variables:

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

Then in your viewController1 create the book by instantiating your Book-class and set it's variables, you could then send your Book-object to your viewController2 .

That would probably be easier for you.

If you just want to change your own code, you could try with:

In your h-file:

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

And in your m-file:

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

The only explanation I see here is, that bookName is not an NSString.

Add the following line to that method:

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

What does XCode write to the log during runtime?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM