在Objective-C中,init方法(即类的指定初始值设定项)和initialize方法之间有什么区别?应该在每个中放入什么初始化代码?
我想子类UIButton添加一些我需要的属性(不是方法...只有属性).
这是我的子类的代码:
//.h-----------------------
@interface MyButton : UIButton{
MyPropertyType *property;
}
@property (nonatomic,retain) MyPropertyType *property;
@end
//.m--------------------------
@implementation MyButton
@synthesize property;
@end
Run Code Online (Sandbox Code Playgroud)
在这里我如何使用该类:
MyButton *btn = ((MytButton *)[MyButton buttonWithType:UIButtonTypeRoundedRect]);
btn.property = SomeDataForTheProperty;
Run Code Online (Sandbox Code Playgroud)
从哪里获得此错误:
-[UIRoundedRectButton setProperty:]: unrecognized selector sent to instance 0x593e920
Run Code Online (Sandbox Code Playgroud)
因此,从ButtonWithType我获得一个UIRoundedRectButton,(Mybutton *)不能施展它...我需要做什么来获得一个MyButton对象?是-init唯一的解决方案?
谢谢!
我试图将UIButton子类化为包含一个活动指示符,但是当我使用initWithFrame :(因为我是uibutton的子类我没有使用buttonWithType :)按钮不显示.在这种情况下我如何设置按钮类型?:
我的视图控制器:
ActivityIndicatorButton *button = [[ActivityIndicatorButton alloc] initWithFrame:CGRectMake(10, 10, 300, 44)];
[button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Older Posts..." forState: UIControlStateNormal];
[cell addSubview:button];
[button release];
Run Code Online (Sandbox Code Playgroud)
我的activityindicatorbutton类:
#import <Foundation/Foundation.h>
@interface ActivityIndicatorButton : UIButton {
UIActivityIndicatorView *_activityView;
}
-(void)startAnimating;
-(void)stopAnimating;
@end
@implementation ActivityIndicatorButton
- (id)initWithFrame:(CGRect)frame {
if (self=[super initWithFrame:frame]) {
_activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_activityView.frame = CGRectOffset(_activityView.frame, 60.0f, 10.0f);
[self addSubview: _activityView];
}
return self;
}
-(void) dealloc{
[super dealloc];
[_activityView release];
_activityView = nil;
}
-(void)startAnimating {
[_activityView startAnimating]; …Run Code Online (Sandbox Code Playgroud) 如果我UIButton在 Storyboard 中创建一个并将其类指定为我创建的子类 ( HighlightTappedButton),则子类中是否有办法将 更改buttonType为Custom?
我知道我可以将类型更改为Custom故事板中的类型,但我不想每次都记住这样做。
UIButtonType custom和system.之间有什么区别?诸如adjustsImageWhenHighlighted影响我所提供的图像的财产将如何影响?
如果我只提供一些图像而不提供其他图像会发生什么?
我是 swift 的新手,我只是想创建 uibutton 的子类。除了当选择按钮时出现这个奇怪的蓝色圆角矩形,如下所示。当我想要的只是一个漂亮的白色边框时。

我班级的代码:
import UIKit
import QuartzCore
@IBDesignable
class ColorButton: UIButton {
//MARK: PROPERTIES
@IBInspectable var stickerColor: UIColor = UIColor.whiteColor() {
didSet {
configure()
}
}
override var selected: Bool {
willSet(newValue) {
super.selected = newValue;
if selected {
layer.borderWidth = 1.0
} else {
layer.borderWidth = 0.0
}
}
}
//MARK: Initializers
override init(frame : CGRect) {
super.init(frame : frame)
setup()
configure()
}
convenience init() {
self.init(frame:CGRectZero)
setup()
configure()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: …Run Code Online (Sandbox Code Playgroud) 我创建了一个UIButton的子类:
//
// DetailButton.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyDetailButton : UIButton {
NSObject *annotation;
}
@property (nonatomic, retain) NSObject *annotation;
@end
//
// DetailButton.m
//
#import "MyDetailButton.h"
@implementation MyDetailButton
@synthesize annotation;
@end
Run Code Online (Sandbox Code Playgroud)
我想我可以通过执行以下操作创建此对象并设置注释对象:
MyDetailButton* rightButton = [MyDetailButton buttonWithType:UIButtonTypeDetailDisclosure];
rightButton.annotation = localAnnotation;
Run Code Online (Sandbox Code Playgroud)
localAnnotation是一个NSObject,但它实际上是一个MKAnnotation.我不明白为什么这不起作用,但在运行时我得到这个错误:
2010-05-27 10:37:29.214 DonorMapProto1[5241:207] *** -[UIButton annotation]: unrecognized selector sent to instance 0x445a190
2010-05-27 10:37:29.215 DonorMapProto1[5241:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIButton annotation]: unrecognized selector sent to instance 0x445a190'
Run Code Online (Sandbox Code Playgroud)
"
我不明白为什么它甚至会查看UIButton,因为我已经将其子类化了,所以应该查看MyDetailButton类来设置该注释属性.我错过了一些非常明显的东西.感觉就像:)
提前感谢您提供的任何帮助
罗斯
uibutton ×5
ios ×4
objective-c ×4
iphone ×2
cocoa ×1
cocoa-touch ×1
init ×1
initializer ×1
subclass ×1
swift ×1