문제

이건 너무 간단 해요 확실합니다! 나는 무언가를 놓치고 그것을 고치려고 노력하는 것에 지쳤다. 누군가가 도울 수 있기를 바랍니다.

artericview.m의 버튼은 작동하지만 버튼은 문자 male.m에 중첩되어 있지 않습니다. 나는 IB를 사용하지 않고 모든 것이 끔찍하게 완료됩니다. 하나의 버튼이 작동하지 않으며 다른 버튼이 작동하지 않습니까?

/////////////////////////////////////////////////////////////////////////////////
 CharacterController.m
/////////////////////////////////////////////////////////////////////////////////
#import "CharacterController.h"
#import "CharacterView.h"

@implementation CharacterController

- (id)init {
    NSLog(@"CharacterController init");
    self = [ super init ];
    if (self != nil) {
    }
    return self;
}

- (void)loadView {
    [ super loadView ];
    characterView = [ [ CharacterView alloc ] init];
    self.view = characterView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (void)dealloc {
    [super dealloc];
}

@end

/////////////////////////////////////////////////////////////////////////////////
 CharacterView.m
/////////////////////////////////////////////////////////////////////////////////

#import "CharacterView.h"
#import "CharacterMale.h"

@implementation CharacterView

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        characterMale = [ [ CharacterMale alloc ] init];
        [self addSubview: characterMale];

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 200, 200, 100);
        [button setImage:[UIImage imageNamed:@"btnCharSelect.png"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(ApplyImage:) forControlEvents:UIControlEventTouchUpInside];
        [ self addSubview: button ];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
}

-(void)ApplyImage:(id)sender
{
    NSLog(@"CharacterView button works");
}

- (void)dealloc {
    [super dealloc];
}

@end

/////////////////////////////////////////////////////////////////////////////////
 CharacterMale.m
/////////////////////////////////////////////////////////////////////////////////

#import "CharacterMale.h"
#import "CharacterController.h"

@implementation CharacterMale

- (id)init {
    self = [ super init];
    if (self != nil) {
        UIImage *image = [UIImage imageNamed:@"charMale.png"];
        imageView = [[ UIImageView alloc] initWithImage:image];
        [image release];
        [ self addSubview: imageView ];

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 200, 100);
        [button setImage:[UIImage imageNamed:@"btnCharSelect.png"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(ApplyImage:) forControlEvents:UIControlEventTouchUpInside];
        [ self addSubview: button ];

    }
    return self;
}

-(void)ApplyImage:(id)sender
{
    NSLog(@"CharacterMal button works");
}

- (void)dealloc {
    [super dealloc];
}

@end
도움이 되었습니까?

해결책

마지막으로!!!!!!

나는 InitwithFrame으로 모든보기를 시작하고 유효한 프레임 낙타로 통과해야했습니다. init는 컨트롤러와 함께 사용해야하며 uiviews에 대한 rects를 통과하는 InitwithFrame을 사용해야합니다 !!!!

characterView = [ [ CharacterView alloc ] initWithFrame:CGRectMake(0, 0, 480, 320)]; 
then 
characterMale = [ [ CharacterMale alloc ] initWithFrame:frame];

다른 팁

고려해야 할 또 다른 것은 그 것입니다 UIButton 추가 된 하위 뷰 a UIImageView 전혀 일하지 마십시오. 당신은 만들어야합니다 UIView 이미지보기와 버튼을 모두 추가합니다.

아마도 이미지보기에서 기본적으로 상호 작용이 꺼져 있기 때문일 것입니다. 그러나 나는 이것을 확인하지 않았습니다.

새로운 견해가 사용자 상호 작용을 수락합니까? 다시 말해, "문자"보기에서 userinteractionenabled가 활성화되어 있습니까?

cgrectzero의 프레임으로 UIView를 초기화하는 동안 버튼을 추가하려고 할 때 비슷한 문제가있었습니다.

@implementation SomeView
...
- (id)initWithTitleImage:(UIImage *) newTitleImage {
  // BROKEN: frame with CGRectZero.
  self = [super initWithFrame:CGRectZero];
  if (self) {
    UIButton *someButton = ...;
    [self addSubview someButton];
  }
}

프레임을 적절한 사각형으로 변경하면 버튼이 작동했습니다.

@implementation SomeView
...
- (id)initWithTitleImage:(UIImage *) newTitleImage {
  // WORKING: frame with proper CGRect.
  self = [super initWithFrame:CGRectMake(0, 0, 480, 320)];
  if (self) {
    UIButton *someButton = ...;
    [self addSubview someButton];
  }
}

나는 DrawRect에서 만든 Uibutton의 선택기와 함께 운이 좋았습니다. UIControlEventTouchDown 인기 대신 UIControlEventTouchUpInside.

나에게 문제는 버튼 프레임의 원점이 상위 프레임보다 크기 때문에 발생했습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top