UIView class properties seemingly disappear when I call drawRect:

好久不见. 提交于 2020-01-17 04:44:05

问题


I am coding an objective-c project with xcode.

I have two important files that are being used: a viewcontroller, and a view. My view is class "PlayingCardView", my viewcontroller is class: "PlayingCardGameViewController".

Here is what PlayingCardView.h looks like:

#import <UIKit/UIKit.h>

@interface PlayingCardView : UIView

@property (nonatomic) NSUInteger rank;
@property (strong, nonatomic) NSString *suit;
@property (nonatomic) BOOL faceUp;

@end

Here is the important code from PlayingCardView.m :

#import "PlayingCardView.h"

@implementation PlayingCardView

-(void)setSuit:(NSString *)suit {
    NSLog(@"suit is set");
    _suit = suit;
    [self setNeedsDisplay];
}

-(void)setRank:(NSUInteger)rank {
    NSLog(@"rank is set");
    _rank = rank;
    [self setNeedsDisplay];
}

-(void)setFaceUp:(BOOL)faceUp {
    _faceUp = faceUp;
    [self setNeedsDisplay];
}

-(NSString *)rankAsString {
    return @[@"?",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"J",@"Q",@"K",@"A"][self.rank];
}

- (void)drawRect:(CGRect)rect {
    NSLog(@"Calling drawRect");
    NSLog(@"%@%@",[self rankAsString],self.suit);
    // Drawing Code
}

@end

Here is what PlayingCardGameViewController.m looks like:

#import "PlayingCardGameViewController.h"

@interface PlayingCardGameViewController ()

@property (weak, nonatomic) IBOutlet PlayingCardView *playingCardView;

@end

@implementation PlayingCardGameViewController

-(void)viewDidLoad {
    NSLog(@"Calling view did load");
    [super viewDidLoad];
    self.playingCardView.suit = @"♥︎";
    self.playingCardView.rank = 13;
}

@end

I included some NSLog's to see the order of what is called, and what is happening. Here is what is printed out:

CardMatchingGame_Assignment04[87470:3734148] Calling view did load CardMatchingGame_Assignment04[87470:3734148] suit is set CardMatchingGame_Assignment04[87470:3734148] rank is set CardMatchingGame_Assignment04[87470:3734148] Calling drawRect CardMatchingGame_Assignment04[87470:3734148] ?(null)

Essentially what this is telling me is first viewDidLoad is called. The suit and rank are set, as they are set in viewDidLoad. Then drawRect is called. Now when I ask to return the suit and rank, I get a null string and a 0-value integer. I have no idea what is happening, but it appears somewhere between viewDidLoad and drawRect the properties are being lost.

Please help me, I am pretty new to objective-c and xcode! All advice is greatly appreciated.

edit: So I have tried to do a few things people suggested, which did not solve the problem. Here is some additional information: The PlayingCardView view in interface builder is set to class PlayingCardView. I tried deleting the IBOutlet line and doing another ctl+drag to recreate the IBOutlet. Nothing has worked so far.


回答1:


This is a nasty one!

I think you have 2 instances of PlayingCardView:

  1. Your view connected to your IBOutlet in Interface Builder
  2. Your (same) view in IB where/because you have not set the class to PlayingCardView.

So that drawRect call is actually happening on the PlayingCardView in your IB when it is loaded from IB and not on the "outlet" view where you are setting the rank and suit.

I am guessing that you might have manually wrote the IBOutlet line instead of doing the dragging.

Check that your PlayingCardView view in Interface builder has its class set to PlayingCardView.

Then run.

If that still does not work, then delete the IBOutlet line from code and from the view inspector (very important) and then drag it again to your PlayingCardGameViewController.h



来源:https://stackoverflow.com/questions/27095594/uiview-class-properties-seemingly-disappear-when-i-call-drawrect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!