当我调用drawRect时,UIView类属性似乎消失了: [英] UIView class properties seemingly disappear when I call drawRect:

查看:93
本文介绍了当我调用drawRect时,UIView类属性似乎消失了:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用xcode编写Objective-C项目。

I am coding an objective-c project with xcode.

我有两个正在使用的重要文件:一个视图控制器和一个视图。
我的视图是 PlayingCardView类,我的视图控制器是 PlayingCardGameViewController类。

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

这是PlayingCardView.h的样子:

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

这是PlayingCardView.m中的重要代码:

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

这是PlayingCardGameViewController.m的样子:

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

我包括了一些NSLog来查看所谓的顺序以及发生的事情。打印内容如下:

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]调用视图确实加载了
CardMatchingGame_Assignment04 [87470:3734148]诉讼设置为
CardMatchingGame_Assignment04 [87470:3734148]等级为
设置CardMatchingGame_Assignment04 [87470:3734148]调用drawRect
CardMatchingGame_Assignment04 [87470:3734148]?(null)

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)

从本质上讲,这告诉我的是第一个viewDidLoad被调用。西装和等级已设置,就像它们在viewDidLoad中设置的一样。然后调用drawRect。现在,当我要求返回西服和等级时,我得到一个空字符串和一个零值整数。我不知道发生了什么,但它似乎在viewDidLoad和drawRect之间的某个位置丢失了。

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.

请帮助我,我对Objective-C和Xcode还是陌生的!

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

编辑:所以我试图做一些人建议的事情,但是并没有解决问题。这是一些其他信息:接口构建器中的 PlayingCardView视图设置为 PlayingCardView类。我尝试删除IBOutlet行,并执行另一个ctl + drag来重新创建IBOutlet。

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.

推荐答案

这太讨厌了!

我认为您有2个PlayingCardView实例:

I think you have 2 instances of PlayingCardView:


  1. 您的视图已连接到Interface Builder中的IBOutlet

  2. 您在IB中的(相同)视图中/因为您尚未将类设置为PlayingCardView。

因此,drawRect调用为实际上是从IB加载时在IB的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.

我想您可能是手动编写了IBOutlet行,而不是进行拖动。

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

检查接口构建器中的 PlayingCardView视图是否将其类设置为 PlayingCardView。

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

然后运行。

如果仍然不起作用,则从代码和视图检查器中删除IBOutlet行(非常重要)然后将其再次拖动到您的PlayingCardGameViewController。 h

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

这篇关于当我调用drawRect时,UIView类属性似乎消失了:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆