2013-06-01 47 views
0

我已經在Objective-C中編寫了一個基本應用程序,該應用程序顯示用戶與圖片的聯繫人。問題是,沒有條塊分割,我覺得我違反了某些標準。此代碼是否遵循標準的Objective-C和MVC約定?

一切都通過一個主ViewController處理,主要是在用戶點擊一個按鈕(通過一個插座到IB)後開始工作。

// 
// ViewController.m 
// Future Take 4 
// 
// Created by Rooz Mahdavian on 5/31/13. 
// Copyright (c) 2013 Rooz Mahdavian. All rights reserved. 
// 

#import "ViewController.h" 
#import <AddressBook/AddressBook.h> 
#import <AddressBookUI/AddressBookUI.h> 
#import <QuartzCore/QuartzCore.h> 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userAllowedForAccess)]; 
    singleTap.numberOfTapsRequired = 1; 
    singleTap.numberOfTouchesRequired = 1; 
    [[self.view viewWithTag:300] addGestureRecognizer:singleTap]; 
    [[self.view viewWithTag:300] setUserInteractionEnabled:YES]; 

    CGRect tempButton = [self.view viewWithTag:300].frame; 
    tempButton.origin.y = self.view.frame.size.height/2 - 56; 
    [self.view viewWithTag:300].frame = tempButton; 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration: .4]; 
    [UIView setAnimationDelay: 0]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 

    CGRect tempButton2 = [self.view viewWithTag:300].frame; 
    tempButton2.origin.y += 90; 
    [self.view viewWithTag:300].frame = tempButton2; 

    [self.view viewWithTag:100].alpha = 1; 

    [UIView commitAnimations]; 


} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


- (IBAction)userAllowedForAccess { 
    NSLog(@"User Has Verified Contacts"); 


    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 55)]; 
    header.backgroundColor = [UIColor colorWithRed:34/255.0 green:34/255.0 blue:34/255.0 alpha:1]; 

    [header.layer setShadowColor:[UIColor colorWithRed:14/255.0 green:14/255.0 blue:14/255.0 alpha:.5].CGColor]; 
    [header.layer setShadowOpacity:0]; 
    [header.layer setShadowRadius:3.0]; 
    [header.layer setShadowOffset:CGSizeMake(2.0, 2.0)]; 

    [self.view addSubview:header]; 
    [self.view bringSubviewToFront:[self.view viewWithTag:100]]; 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration: .7]; 
    [UIView setAnimationDelay: 0]; 


    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 

    CGRect tempLogo = [self.view viewWithTag:100].frame; 
    tempLogo.origin.y = -14; 
    tempLogo.size.height = tempLogo.size.height/2.5; 
    tempLogo.size.width = tempLogo.size.width/2.5; 
    tempLogo.origin.x = 160 - tempLogo.size.width/2; 
    [self.view viewWithTag:100].frame = tempLogo; 

    CGRect tempButton = [self.view viewWithTag:300].frame; 
    tempButton.size.width = tempButton.size.width * 2.5; 
    tempButton.size.height = tempButton.size.height * 2.5; 
    tempButton.origin.x = -((tempButton.size.width/2)-((tempButton.size.width * 2.5))/2); 

    [self.view viewWithTag:300].frame = tempButton; 
    [self.view viewWithTag:300].alpha = 0; 


    [self.view viewWithTag:200].alpha = 0; 
    [header.layer setShadowOpacity:1]; 

    [UIView commitAnimations]; 





    ABAddressBookRef addressBook = ABAddressBookCreate(); 
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height)]; 




    __block BOOL accessGranted = NO; 
    if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6 
     dispatch_semaphore_t sema = dispatch_semaphore_create(0); 
     ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { 
      accessGranted = granted; 
      dispatch_semaphore_signal(sema); 
     }); 
     dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); 
    } 


    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook); 
    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook); 


    int peopleInARow = 0; 
    int maxPeopleInARow = 3; 
    int positionFromTop = 20; 
    int IMAGE_SIZE = 70; 
    float animationOffset = .5; 
    float animationOffsetChange = .3; 
    float animationDuration = .5; 
    int scaleOffset = 40; 
    int screenWidth = 320; 
    int startingPositionFromLeft = 26; 
    int positionFromLeft = startingPositionFromLeft; 
    int topOffset = 40; 
    int leftOffset = 26; 
    int numberOfRows = 0; 


    UIView *contactContainer; 
    UIImage* image; 
    CALayer * l; 
    NSString* name; 
    NSString* lastName; 
    NSString* firstName; 
    UIImageView *newimageview; 
    UILabel *label; 
    UIView *contactImageContainer; 



    for (int i = 0; i < nPeople; i++) 
    { 
     ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i); 
     firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person, 
               kABPersonFirstNameProperty); 
     lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person, 
               kABPersonLastNameProperty); 
     name = [NSString stringWithFormat:@"%@ %@", firstName, lastName]; 





     if(ABPersonHasImageData(person)){ 
      //NSLog(@"%@", name); 
      image = [UIImage imageWithData:(__bridge NSData *)ABPersonCopyImageData(person)]; 

      //NSLog(@"Position %i", positionFromLeft); 
      newimageview = [[UIImageView alloc] initWithFrame:CGRectMake(-scaleOffset/2, -scaleOffset/2, IMAGE_SIZE+scaleOffset, IMAGE_SIZE+scaleOffset)]; 
      newimageview.contentMode = UIViewContentModeScaleAspectFit; 




      [newimageview setImage: image]; 


      contactContainer = [[UIView alloc] initWithFrame:CGRectMake(positionFromLeft, positionFromTop + 20, IMAGE_SIZE, 200)]; 
      contactImageContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, IMAGE_SIZE, IMAGE_SIZE)]; 
      contactImageContainer.clipsToBounds = YES; 

      l = [contactImageContainer layer]; 
      [l setMasksToBounds:YES]; 
      [l setCornerRadius:IMAGE_SIZE/2]; 

      // You can even add a border 
      [l setBorderWidth:0.0]; 
      [l setBorderColor:[[UIColor colorWithRed:234.0/255.0 green:234.0/255.0 blue:234.0/255.0 alpha:.6] CGColor]]; 


      [contactImageContainer addSubview:newimageview]; 







      [contactContainer addSubview:contactImageContainer]; 

      label = [[UILabel alloc] initWithFrame: CGRectMake(0, IMAGE_SIZE + 10, IMAGE_SIZE, 20)]; 
      label.text = firstName; 
      label.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0]; 
      label.textColor = [UIColor whiteColor]; 
      [label setTextAlignment:NSTextAlignmentCenter]; 
      [label setFont:[UIFont fontWithName:@"Arial-BoldMT" size:14]]; 
      [contactContainer addSubview:label]; 


      contactContainer.alpha = 0; 






      [UIView beginAnimations:nil context:nil]; 
      [UIView setAnimationDuration:animationDuration]; 
      [UIView setAnimationDelay:animationOffset]; 
      animationOffset+= animationOffsetChange; 
      [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 

      contactContainer.alpha = 1; 
      CGRect temp = contactContainer.frame; 
      temp.origin.y = positionFromTop; 
      contactContainer.frame = temp; 

      [UIView commitAnimations]; 

      if(peopleInARow >= 2){ 
       positionFromTop += IMAGE_SIZE + topOffset; 
       peopleInARow = 0; 
       positionFromLeft = startingPositionFromLeft; 
       numberOfRows++; 

      } else { 
       peopleInARow += 1; 
       positionFromLeft += IMAGE_SIZE + leftOffset; 
      } 



      [scroll addSubview:contactContainer]; 
      [scroll bringSubviewToFront:contactContainer]; 


     } 

    } 

    NSLog(@"%i", numberOfRows); 

    scroll.contentSize = CGSizeMake(screenWidth, 150 * numberOfRows); 
    scroll.pagingEnabled = NO; 

    [self.view addSubview:scroll]; 
    [self.view bringSubviewToFront:scroll]; 


} 

- (IBAction)userDeniedAccess:(id)sender { 
    NSLog(@"Denied"); 
} 

@end 

誰能告訴我重寫這個的最好方法和什麼標準/約定(可能在MVC中)我做錯了嗎?

+0

這應該張貼在http://codereview.stackexchange.com,而不是stackoverflow。 – rmaddy

回答

3

你沒有違反任何事情的MVC。視圖控制器加載一些聯繫人並將其顯示在視圖中。由聯繫人表示的模型和視圖根本沒有耦合。從MVC的角度來看,這是正確的。

正如你所說,這是一個簡單的應用程序。如果應用程序變得更大更復雜,您可能需要爲代碼添加一些設計。但是這種設計對於這種複雜程度很有好處。例如,如果這個類變得更大更復雜,您可以將聯繫人加載部分分成一個新類。

但是,您的代碼存在非常糟糕的一面。方法userAllowedForAccess包含100多行代碼。模塊化必須保持在類的級別以及方法和功能。根據其執行的功能將代碼分成許多方法。例如:

UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 55)]; 
header.backgroundColor = [UIColor colorWithRed:34/255.0 green:34/255.0 blue:34/255.0 alpha:1]; 

[header.layer setShadowColor:[UIColor colorWithRed:14/255.0 green:14/255.0 blue:14/255.0 alpha:.5].CGColor]; 
[header.layer setShadowOpacity:0]; 
[header.layer setShadowRadius:3.0]; 
[header.layer setShadowOffset:CGSizeMake(2.0, 2.0)]; 

[self.view addSubview:header]; 
[self.view bringSubviewToFront:[self.view viewWithTag:100]]; 

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration: .7]; 
[UIView setAnimationDelay: 0]; 


[UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 

可以在一個名爲- (void) setupHeaderView方法提取,你可以做的另一種方法來加載接觸- (NSArray *) getContacts或只是- (NSArray *) contacts匹配的Objective-C對干將定義風格。

+0

創建的,我將它分成了大約10種不同的方法。還有什麼其他的說明? – roozbubu

+1

有一件小事。爲什麼不使用'xib'或'storyboard'文件來加載你的視圖?這可以從你的類中提取大量的代碼。 –

+0

我使用故事板來創建基本視圖和一些靜態圖像,但主要是其他所有內容(聯繫人視圖)都是動態創建的。我可以移動的是滾動視圖和標題 - 謝謝 – roozbubu

2

我將開始重構你的

- (IBAction)userAllowedForAccess 

方法在較小的和原子的方法。有一種方法基本上可以做到,通常是一個糟糕的主意。

+0

陷入困境,做到了。將其分解成9-10個較小的方法,並創建全局屬性以更輕鬆地管理事物。但它看起來並沒有看到我違反了一些巨大的規則或​​慣例? – roozbubu

+1

你需要了解的基本上是你的View Controller的角色是管理你的視圖和你的模型之間的交互。你可以將模型相關的東西(AddressBook fetch)分離到另一個類中,也可以將動畫(與視圖相關的東西)放到另一個地方。 –

+0

我正在將AddressBook代碼移動到一個新的專用模型中,但我不太確定在哪裏可以移動View相關的代碼,因爲它是通過IB – roozbubu

相關問題