2014-02-14 90 views
0

我想在我的UIViewController中正確地重構方法。我對Objective-C相當陌生,所以試圖理解這裏最好的方法。該方法是在屏幕上創建一個自定義底部欄。這是一個UIView,並有多個子視圖和自動佈局。重構在UIViewController中創建自定義UIView的大型方法

目前酒吧的整個創作都是採用一種方法,約300行。理想情況下,我想將其移入子類,但需要了解如何正確完成此操作。

下面是該方法的開始感受一下它的作用:

-(void)setupBottomBarViewForOrientation:(UIInterfaceOrientation)forOrientation { 
UIView *bottomBarInnerView = [UIView autolayoutView]; 
[self.bottomBarView addSubview:bottomBarInnerView]; 

NSDictionary *views = NSDictionaryOfVariableBindings(bottomBarInnerView); 
NSDictionary *bottomBarMetrics = @{@"minVPadding":@2.0}; 
[self.bottomBarView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(minVPadding)-[bottomBarInnerView]-(minVPadding)-|" options:0 metrics:bottomBarMetrics views:views]]; 
[self.bottomBarView addConstraint:[NSLayoutConstraint constraintWithItem:bottomBarInnerView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.bottomBarView attribute:NSLayoutAttributeCenterX multiplier:1 constant:1]]; 

NSString *word = @"Word"; 
NSUInteger letterCount = [word length]; 

NSMutableArray *viewList = [[NSMutableArray alloc] init]; // Create an array to store the tiles within 
NSDictionary *metrics = @{@"letterTileMinHeight":@40.0,@"padding":@3.0,@"letterViewPadding":@5.0}; // Constraint metrics 
... [OTHER LOGIC] which create multiple subviews further UIViews and UILabels ... 

是將它變成UIView子類的最佳途徑?那麼定製代碼在哪裏(哪個方法)?另外它是如何從VC調用並創建自動佈局的?

我明白子類等,主要問題是如何創建自動佈局約束。一些後來的自動佈局約束從UIViewController鏈接到self.view,所以這個視圖應該傳遞到創建橫幅欄子視圖的新方法中?

回答

1

你應該絕對讓它成爲一個UIView子類。通過你需要的任何配置。看起來像一本字典,現在,所以你可以打電話給你的init()方法類似

-(MyNewView*)initWithDictionary:(NSDictionary*)dict {} 

視圖子類將設立自己的約束條件是它裏面的東西。如果使用它的視圖控制器在整個子視圖上需要約束,那麼它應該添加它們,但不應該混淆它。

下面是一個子視圖init方法的示例,該方法需要字符串& count,創建兩個標籤以顯示它們,並將它們添加到自身。您可以佈置子視圖或自動佈局以將它們放置在新視圖中。

-(BottomBarInnerView *)initWithString:(NSString *)string count:(NSNumber*)count { 
    self = [super initWithFrame:CGRectZero]; 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)]; 
    label.text = string; 
    [self addSubview:label]; 

    UILabel *countLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 20, 10)]; 
    countLabel.text = [count stringValue]; 
    [self addSubview:countLabel]; 

    return self; 
} 

由於視圖使用CGRectZero你需要在創建它來調整其大小,所以它會是這樣的:

BottomBarInnerView* bottomBarInnerView = [[BottomBarInnerView alloc] initWithString:@"Hi Mom" count:@(42)]; 
bottomBarInnerView.frame = CGRectMake(0, 200, 100, 50); 
[self.view addSubview:bottomBarInnerView]; 

另外,您可以添加約束它,並讓自動佈局集爲你的框架。

+0

好吧,我創建'UIView'的子視圖然後在init方法內調用initWithDictionary或任何我最終使用?我已經看到它與layoutSubViews等完成,我應該在實際的子視圖類中使用什麼方法/方法? – StuartM

+0

在你的initWithDictionary中,你會做self = [super init],它使你的對象變成一個UIView。在此之後,做任何你需要做的事情來使它成爲你的。當然,添加子視圖,然後將它們放在layoutSubviews中,或使用自動佈局,這看起來像你最初想做的。 – jsd

+0

嘿@jsd。這裏的實際要求是從問題中製作'bottomBarInnerView'。該字典將不再需要,因爲該字典用於該視圖上的約束。我需要繼承'UIView'來創建'bottomBarInnerView'。所以字符串,計數等將被傳遞到相關的新的init方法和代碼[AFTER]上面的大塊。無論如何,問題是我如何繼承UIView並正確配置它。即我子類'UIView',然後調用alloc/init - 然後把所有的代碼之後。或者我創建一個自定義的'initWithString'方法,然後調用... – StuartM