2017-03-31 22 views
0

我正在嘗試製作一個delegate,它將在UIView的定製UIView內發送來自UIButtons的消息,並且在錯誤消息之後有點超出我的深度。我最近學會了如何獲得delegate to send messages successfully from within an instance method成員參考基本類型'類'不是一個結構或聯盟 - Fixit是否讓我朝着正確的方向發展?

SGView包括佈置在一個圓的多個UIButtons,並設計成通過不同UIViews.The geometry works when the method is called from a parent UIView被用於一個類方法。現在我已經加入@protocol@property到類方法聲明

像這樣宣佈delegate ...

SGView.h

#import <UIKit/UIKit.h> 

@protocol SGViewDelegate <NSObject> 
    -(void)buttonPressed:(UIButton*)button; 
@end 

@interface SGView : UIView { 
    } 
    +(id)circleOfButtons:(int)buttonCount buttonSize:(CGFloat)buttonSize circleRadius:(CGFloat)circleRadius; 

@property (assign) id<SGViewDelegate> delegate; 

@end 

SGView.m基本上是這樣的

#import "SGView.h" 

    @implementation SGView : UIView 

    +(id)circleOfButtons:(int)buttonCount buttonSize:(CGFloat)buttonSize circleRadius:(CGFloat)circleRadius 
    { 
    UIView *multipleViews  = [self new]; 

    // … circular geometry … 

    [circleButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 

    [multipleViews addSubview:circleButton]; 
    } 
    return multipleViews; 
    } 
    @end 

我改變從self目標到self.delegate上述目標的行動聲明的那一刻, Xcode中報告的三個錯誤

定義「objc_class」的

必須從模塊 「ObjectiveC.runtime」導入之前,需要它

否「結構objc_class」命名的「委託」構件

會員引用類型「結構objc_class *」是一個指針;你的意思是 使用' - >'?

的Xcode提供了一個皿在過去的這些

Fixit Replace」.」 with 「->」 

通過接受皿我刪除了三個問題,並介紹了另

會員參考基礎型「類」不是一個結構或工會

如果這Fixit帶領我在正確的方向,究竟是什麼我接下來要做什麼?

我需要問這個問題,以瞭解什麼可能是一個直截了當的問題,然後再回到Apple documentationthese tutorials試圖解決它。任何幫助最受讚賞。謝謝。

SOLUTION

這可以在我的答案我自己的問題被發現。

回答

0

將類方法更改爲實例方法似乎解決了我的問題。

這涉及使類方法+(id)circleOfButtons:... etc成在兩個.m.h和文件的實例方法(-(id)circleOfButtons:... etc)。此外,還必須從

SGView *sgView    = [SGView circleOfButtons:count buttonSize:size circleRadius:radius]; 

UIView改變呼叫

SGView *sgView    = [[SGView alloc] circleOfButtons:count buttonSize:size circleRadius:radius]; 

UIView *multipleViews  = [self new]; 

改變SGView的聲明

UIView *multipleViews  = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds]; 

SGView可以從不同的UIViews該改變的數量和佈置成圓形的UIButtons尺寸現在再利用。在SGView該代表還允許每個按鈕發送標記消息到另一個類的方法 - 在我的情況下,ViewController.

的完整列表包含下面,我會歡迎在OO更多的經驗,從別人的建議或改進節目。

ViewController.h

#import <UIKit/UIKit.h> 

    @interface ViewController : UIViewController <UIContentContainer> { 
    } 
@end 

ViewController.m

#import "ViewController.h" 
#import "FamilyView.h" 

@interface ViewController() 

@end 

@implementation ViewController 

    - (void)viewDidLoad            { 
    [super viewDidLoad]; 

    NSLog(@"load FamilyView"); 

    CGRect rect    = [UIScreen mainScreen].bounds; 
    float statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height; 
    CGRect screenFrame  = CGRectMake(0, statusBarHeight, rect.size.width, rect.size.height - statusBarHeight); 
    self.view    = [[UIView alloc] initWithFrame: screenFrame]; 

    FamilyView *cv   = [[FamilyView alloc]initWithFrame:screenFrame]; 
    [self.view addSubview:cv];  
} 


    - (void)buttonPressed:(UIButton*)button       { 
    NSLog(@"Button %ld clicked.", (long int)[button tag]); 

    switch (button.tag) { 
     case 1: 
     //   [self goToFamily1]; 
      break; 
     case 2: 
     //   [self goToFamily2]; 
      break; 
     case 3: 
     //   [self goToFamily3]; 
      break; 
     case 4: 
     //   [self goToFamily4]; 
      break; 
     case 5: 
     //   [self goToFamily5]; 
      break; 
     default: 
     //   [self goToHelp]; 
      break; 
     } 
    } 
    @end 

FamilyView.h

#import <UIKit/UIKit.h> 
#import "ViewController.h" 
#import "SGView.h" 

    @interface FamilyView : UIView { 
    } 
@end 

FamilyView.m

#import <UIKit/UIKit.h> 
#import "FamilyView.h" 
#import "SGView.h" 

    @implementation FamilyView : UIView 

    - (id)initWithFrame:(CGRect)frame { 
    self         = [super initWithFrame:[UIScreen mainScreen].bounds]; 
    if (self) { 

    self.backgroundColor     = [UIColor lightGrayColor]; 

    int  count       = 5; //16; 
    CGFloat size       = 80; //41; 
    CGFloat radius       = 68; //105; 

    SGView *sgView       = [[SGView alloc] circleOfButtons:count buttonSize:size circleRadius:radius]; 
    [self addSubview:sgView]; 
    } 
    return self; 
} 
@end 

SGView.h

#import <UIKit/UIKit.h> 

    @protocol SGViewDelegate <NSObject> 
    -(void)buttonPressed:(UIButton*)button; 
    @end 

    @interface SGView : UIView { 
    } 

     -(id)circleOfButtons:(int)buttonCount buttonSize:(CGFloat)buttonSize circleRadius:(CGFloat)circleRadius; 

    @property (assign) id<SGViewDelegate> delegate; 

    @end 

SGView.m

#import "SGView.h" 

    @implementation SGView : UIView 

    -(id)circleOfButtons:(int)buttonCount buttonSize:(CGFloat)buttonSize circleRadius:(CGFloat)circleRadius 
    { 
    UIView *multipleViews     = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds]; 

    CGPoint screenCentre; 
    CGPoint buttonCentre; 

    screenCentre.x       = CGRectGetWidth ([UIScreen mainScreen].bounds)/2; 
    screenCentre.y       = CGRectGetHeight ([UIScreen mainScreen].bounds)/2; 

    for (int i = 1; i <= buttonCount; i++) { 
     CGFloat radians      = 2 * M_PI * i/buttonCount; 
     CGFloat arcStartPoint    = - M_PI/2; // first point clockwise after 12 o'clock 
     buttonCentre.x      = screenCentre.x + circleRadius * cos(radians + arcStartPoint); 
     buttonCentre.y      = screenCentre.y + circleRadius * sin(radians + arcStartPoint); 

     CGPoint target      = CGPointMake(buttonCentre.x, buttonCentre.y); 
     CGFloat x       = screenCentre.x - buttonSize/2; 
     CGFloat y       = screenCentre.y - buttonSize/2; 
     CGFloat wide      = buttonSize; 
     CGFloat high      = buttonSize; 
     UIButton *circleButton    = [[UIButton alloc] initWithFrame:CGRectMake(x, y, wide, high)]; 

     [circleButton setTag:i]; 
     [circleButton addTarget:self.delegate action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 

     circleButton.clipsToBounds   = YES; 
     circleButton.layer.masksToBounds = NO; 
     circleButton.layer.shadowOffset  = CGSizeMake(-15, 20); 
     circleButton.layer.shadowRadius  = 5; 
     circleButton.layer.shadowOpacity = 0.0; 
     circleButton.layer.borderWidth  = 0.25f; 
     circleButton.layer.cornerRadius  = buttonSize/2; 
     circleButton.layer.borderColor  = [UIColor blackColor].CGColor; 
     circleButton.backgroundColor  = UIColor.whiteColor; 
     [circleButton setTitle:[NSString stringWithFormat:@"%i", i] forState:UIControlStateNormal]; 

     if (buttonCount > 25) { 
      [circleButton setTitleColor: [UIColor clearColor] forState:UIControlStateNormal]; 
      } else { 
      [circleButton setTitleColor: [UIColor grayColor] forState:UIControlStateNormal]; 
     } 

     [multipleViews addSubview:circleButton]; 

    // animation 1 
     [UIView animateWithDuration:0.5 animations:^{ 
     circleButton.transform    = CGAffineTransformMakeScale(1.0, 1.0); 
     circleButton.center     = screenCentre; 
     } 
        completion:^(BOOL finished){}]; 

    // animation 2 
     [UIView animateWithDuration:0.5f animations:^{ 
     circleButton.transform    = CGAffineTransformIdentity; 
     circleButton.center     = target; 
     } 
        completion:^(BOOL finished){}]; 
    } 
    return multipleViews; 
} 
@end 
相關問題