2011-11-07 614 views
2

我準備通過繼承UIAlerView來提交這樣的定製。它的佈局完全基於UIAlertView的給定地形,沒有讀取任何私有屬性。 App Store審覈過程是否可以接受這種定製?蘋果是否允許自定義UIAlertView?

enter image description here

BGAlertViewWithSwitch.h

// 
// BGAlertViewWithSwitch.h 
// BGAlertViewWithSwitch 
// 
// Created by Borbas Geri on 11/7/11. 
// Copyright 2011 ©ompactApps. All rights reserved. 
// 

#import <Foundation/Foundation.h> 


//An assumed value. 
#define ALERT_VIEW_LINE_HEIGHT 20.0 
#define ALERT_VIEW_LABEL_PADDING 5.0 
#define ALERT_VIEW_LABEL_ALPHA 0.5 

#define kAlertSwitchLabelTag 42 


@interface BGAlertViewWithSwitch : UIAlertView 
{ 
    UISwitch *_alertSwitch; 
    UILabel *_alertSwitchLabel; 
} 
@property (nonatomic, retain) UISwitch *alertSwitch; 
@property (nonatomic, retain) UILabel *alertSwitchLabel; 
@property (nonatomic, readonly, getter=isOn) BOOL on; 

-(id)initWithTitle:(NSString*) title 
      message:(NSString*) message 
    switchMessage:(NSString*) switchMessage 
      delegate:(id) delegate 
cancelButtonTitle:(NSString*) cancelButtonTitle 
    okButtonTitle:(NSString*) okButtonTitle; 

@end 

BGAlertViewWithSwitch.m

// 
// BGAlertViewWithSwitch.m 
// BGAlertViewWithSwitch 
// 
// Created by Borbas Geri on 11/7/11. 
// Copyright 2011 ©ompactApps. All rights reserved. 
// 


#import "BGAlertViewWithSwitch.h" 


@implementation BGAlertViewWithSwitch 
@synthesize alertSwitch = _alertSwitch; 
@synthesize alertSwitchLabel = _alertSwitchLabel; 


#pragma mark - UISwitch Accessor 

-(BOOL)isOn 
{ 
    return self.alertSwitch.isOn; 
} 


#pragma mark - View lifecycle 

-(id)initWithTitle:(NSString*) title 
      message:(NSString*) message 
    switchMessage:(NSString*) switchMessage 
      delegate:(id) delegate 
cancelButtonTitle:(NSString*) cancelButtonTitle 
    okButtonTitle:(NSString*) okButtonTitle 
{ 

    //For testing layout 
    NSString *placeHolder = @""; 

    //Append a line to the message that leaves the place for the switch. 
    NSString *_expandedMessage = [NSString stringWithFormat:@"%@\n%@\n%@\n", message, placeHolder, placeHolder]; 

    if (self = [self initWithTitle:title 
          message:_expandedMessage 
          delegate:delegate 
       cancelButtonTitle:cancelButtonTitle 
       otherButtonTitles:okButtonTitle, nil]) 
    { 
     //Add switch. 
     self.alertSwitch = [[UISwitch alloc] init]; 
     self.alertSwitch.on = YES; 
     [self addSubview:self.alertSwitch]; 

     //Add label. 
     self.alertSwitchLabel = [[UILabel alloc] init]; 
     self.alertSwitchLabel.text = switchMessage; 
     self.alertSwitchLabel.tag = kAlertSwitchLabelTag; 
     [self addSubview:self.alertSwitchLabel]; 
    } 
    return self; 
} 

- (void)dealloc 
{ 
    self.alertSwitch = nil; 
    self.alertSwitchLabel = nil; 

    [super dealloc]; 
} 


#pragma mark - Topography 

- (void)layoutSubviews 
{ 
    NSLog(@"layoutSubviews to (%@)", NSStringFromCGRect(self.frame)); 

    //Weak link to the message label. 
    UILabel *messageLabel; 

    //Enumerate subviews to find message label (the base of the topography). 
    for (UIView *eachSubview in self.subviews) 
     if ([[eachSubview class] isEqual:[UILabel class]]) 
     { 
      UILabel *eachLabel = (UILabel*)eachSubview; 
      if (eachLabel.tag != kAlertSwitchLabelTag) 
      { 
       messageLabel = eachLabel; 
       NSLog(@"Each label frame (%@), saying '%@'", NSStringFromCGRect(eachLabel.frame), eachLabel.text);     
      } 
     } 

    //Center new content. 
    CGSize alertSwitchLabelSize = [self.alertSwitchLabel.text sizeWithFont:messageLabel.font]; 
    float horizontalCentering = (messageLabel.frame.size.width - (alertSwitchLabelSize.width + ALERT_VIEW_LABEL_PADDING + self.alertSwitch.frame.size.width))/2; 


    //Switch goes to the bottom right. 
    float switchVerticalCentering = ((ALERT_VIEW_LINE_HEIGHT * 2 + 1) - self.alertSwitch.frame.size.height)/2; 
    CGRect alertSwitchFrame = CGRectMake(messageLabel.frame.origin.x + messageLabel.frame.size.width - self.alertSwitch.frame.size.width - horizontalCentering, 
             messageLabel.frame.origin.y + messageLabel.frame.size.height - self.alertSwitch.frame.size.height - switchVerticalCentering, 
             self.alertSwitch.frame.size.width, 
             self.alertSwitch.frame.size.height); 
    self.alertSwitch.frame = alertSwitchFrame; 

    //Label goes to the bottom left.  
    float switchLabelVerticalCentering = ((ALERT_VIEW_LINE_HEIGHT * 2 + 1) - ALERT_VIEW_LINE_HEIGHT)/2; 
    CGRect alertSwitchLabelFrame = CGRectMake(round(messageLabel.frame.origin.x + horizontalCentering), 
               round(messageLabel.frame.origin.y + messageLabel.frame.size.height - ALERT_VIEW_LINE_HEIGHT - switchLabelVerticalCentering), 
               messageLabel.frame.size.width - self.alertSwitch.frame.size.width, 
               ALERT_VIEW_LINE_HEIGHT); //self.alertSwitchLabel.frame.size.height); 
    self.alertSwitchLabel.frame = alertSwitchLabelFrame; 

    //Copy message label properties. 
    self.alertSwitchLabel.backgroundColor = [UIColor clearColor]; 
    self.alertSwitchLabel.textColor = messageLabel.textColor; 
    self.alertSwitchLabel.font = messageLabel.font; 
    self.alertSwitchLabel.shadowColor = messageLabel.shadowColor; 
    self.alertSwitchLabel.shadowOffset = messageLabel.shadowOffset; 

    //Weaken. 
    self.alertSwitchLabel.alpha = ALERT_VIEW_LABEL_ALPHA; 

    [super layoutSubviews]; 
} 


@end 
+0

嘿,只是一個問題。我可以在我的項目中使用你的代碼嗎?我需要完全一樣的東西。非常感謝 – Youssef

回答

3

沒有人,但蘋果可以充分地回答這個問題,所以最好的辦法是把它放到測試。我認爲您必須問自己的主要問題是:我違反了Apple開發人員協議中的任何規定嗎?如果沒有,請提交您的應用程序。如果您擔心拒絕,請考慮採取其他方式作爲備份,並準備在出現問題時提交。

不是你問過,但我也會認爲蘋果設計的這種改變不是很直觀。你的意思是轉換意思是「也從moquus中刪除?」因爲你已經有一個大的刪除按鈕。如果開關關閉,那麼刪除按鈕刪除了什麼?

+0

太好了,謝謝。我將包括「也」一詞。順便說一句,我只是把這個頁面發佈到蘋果的一個問題上,所以很快我就會回答。 – Geri

+0

我改變了上面屏幕截圖中的單詞。 :) – Geri

+0

葉'這不是太「自然」,但比從零開始創建一個UIAlertView更好。 – Geri

4

的實際問題的答案是沒有 - 蘋果不允許UIAlertView中要被繼承。從UIAlertView中的文檔:

子類注

的UIAlertView中類擬用作原樣,並且不支持 子類。該類的視圖層次結構是私有的,並且不得修改 。

這裏找到: Subclassing UIAlertView