سؤال

وهذا هو الحال لعنة ايم بسيط على يقين! ايم شيء مفقود والتراسل الفوري استنفدت من محاولة إصلاحه. نأمل أن نجد المساعدة.

ووزر في CharacterView.m يعمل ولكن زر متداخلة عليها في CharacterMale.m لا. أنا لا تستخدم يتم IB كل شيء progmatically. ما الذي يسبب زر واحد للعمل والآخر لا؟

/////////////////////////////////////////////////////////////////////////////////
 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 ويمر في rects إطار صالحة. التهيئة ينبغي أن تستخدم مع وحدات تحكم وinitWithFrame تمر rects لUIViews !!!!

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

نصائح أخرى

وشيء آخر للنظر هو أن subviews UIButton التي تمت إضافتها على UIImageView لا تعمل على الإطلاق. تحتاج إلى إنشاء UIView التي تضيفها كل من عرض صورة والأزرار ل.

وهذا هو الأرجح لأن يتم تشغيل التفاعل إيقاف افتراضيا على وجهات نظر الصورة، ولكني لا تحديد هذا.

هل تقبل طريقة العرض الجديدة تفاعل المستخدم؟ وبعبارة أخرى، userInteractionEnabled تمكين على عرض "حرف"؟

وكان لي مشكلة مشابهة عندما حاولت إضافة الزر أثناء تهيئة لUIView مع إطار من CGRectZero:

@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];
  }
}

ولقد كان الحظ مع محدد لUIButton إنشاؤها في drawRect باستخدام UIControlEventTouchDown بدلا من UIControlEventTouchUpInside شعبية.

وبالنسبة لي كان سبب المشكلة نظرا لأن كان أصل إطار زر أكبر ثم الإطار الأصل.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top