【问题标题】:initWithFrame doesnt work on my project in IOS 5 but did in IOS 4initWithFrame 不适用于我在 IOS 5 中的项目,但在 IOS 4 中可以
【发布时间】:2012-01-26 19:24:16
【问题描述】:

我根据 ios 4 的教程制作了一个程序,它是一个复选框。我有一个朋友开始用 xcode for iphone 编程。所以,我给了他我的代码,这样他就可以开始了。事实证明,他试图按照我为 IOS4 编写的代码在 ios5 中自己编写代码。他一直告诉我他没有工作,并且他遇到了一堆错误,如下所示。所以我会打开我的代码并在我更新的 xcode 上运行它,它运行良好,没问题。 有趣的是,当我到达他家时,我查看了代码并玩弄了它。他是对的。尝试使用新 ios 在新 xcode 中执行新项目中的代码,该项目会引发各种错误。我在底部贴了一些。

那么,我的问题是……这是为什么呢? 这是否与 ios5 使用 ARC 和不同属性的方式有关?

  • NSObject 可能无法响应 initWithFrame
  • 实例方法 - 未找到 setImageForState(返回类型默认为“id”)
  • 实例方法 - addTarget:action:forControlEvents not found(返回类型默认为 id)
  • MicheckBox 可能无法响应 initWithFrame 还有更多

我的代码如下 必须 png 一个名为 checkbox_ticked.png 和一个 checkbox_not_ticked.png

micheckbox.h

#import <UIKit/UIKit.h>
@interface MICheckBox : UIButton 
{
BOOL isChecked;
}
@property (nonatomic,assign) BOOL isChecked;
-(IBAction) checkBoxClicked;
@end

micheckbox.m

#import "MICheckBox.h"
@implementation MICheckBox
@synthesize isChecked;
- (id)initWithFrame:(CGRect)frame 
{
    if (self = [super initWithFrame:frame]) 
    {
    [self setImage:[UIImage imageNamed:@"checkbox_not_ticked.png"] forState:UIControlStateNormal];
[self 
addTarget:self action:@selector(checkBoxClicked)forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
-(IBAction) checkBoxClicked
{
if(self.isChecked ==NO)
    {
    self.isChecked =YES;
    [self setImage:[UIImage imageNamed:@"checkbox_ticked.png"] forState:UIControlStateNormal];

    }
    else
        {
            self.isChecked =NO;
        [self setImage:[UIImage  imageNamed:@"checkbox_not_ticked.png"]forState:UIControlStateNormal];
        }
}
- (void)dealloc 
{
    [super dealloc];
}
@end

MICheckBoxAppDelegate.h

#import <UIKit/UIKit.h>
@interface MICheckBoxAppDelegate : NSObject <UIApplicationDelegate> 
{
    UIWindow *window;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end

MICheckBoxAppDelegate.m

#import "MICheckBoxAppDelegate.h"
#import "MICheckBox.h"
@implementation MICheckBoxAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application 
{    
    // Override point for customization after application launch
    MICheckBox *checkBox =[[MICheckBox alloc]initWithFrame:CGRectMake(5, 80, 150, 30)];
    [checkBox setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [checkBox setTitle:@"Checkbox" forState:UIControlStateNormal];
    [window addSubview:checkBox];
    [window makeKeyAndVisible];
}
- (void)dealloc 
{
    [window release];
    [super dealloc];
}
@end

【问题讨论】:

  • 这看起来就像是 Xcode 只是因为错误而疯狂的时候之一。通常,您要查看 Show issue navigator 或 Command-4 并查看列表顶部的错误(红色错误)。还要检查您的导入是否有拼写错误,这些错误似乎表明“MICheckBox.h”标头未正确导入。但是还是先检查一下最上面的错误。
  • 如果我从头开始编码,我会同意你的看法。但是,这是我去年 10 月完成的项目的工作副本。只是将它应用到 xcode 中的一个新项目,它就会弹道并开始左右抛出错误。顺便说一句,只是为了回答你的建议,我确实检查了,一切似乎都与导入有关。
  • 但我同意...这是 xcode 将要疯子的时代之一。

标签: objective-c xcode4 ios4 ios5 xcode4.2


【解决方案1】:

我想你忘了在 MICheckBox 类的 initWithFrame 中“返回自我”。

【讨论】:

  • 它在那里......就像代码看起来你无法分辨的那样。但它就在那里。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多