2011-06-14 44 views
0

我用下面的代碼交換意見如何編寫Objective-C的功能

- (void)loading2menu{ 

[UIView beginAnimations:@"Loading2Menu" context:nil]; 
[UIView setAnimationDuration:1.25]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 

if (self.menuViewController.view.superview == nil) 
{ 
    if (self.menuViewController == nil) 
    { 
     MenuViewController *menuController = 
     [[MenuViewController alloc] initWithNibName:@"MenuView" 
              bundle:nil]; 
     self.menuViewController = menuController; 
     [menuController release]; 
    } 

    [UIView setAnimationTransition: 
    UIViewAnimationTransitionCurlUp 
          forView:self.view cache:YES]; 

    [loadingViewController viewWillAppear:YES]; 
    [menuViewController viewWillDisappear:YES]; 
    [loadingViewController.view removeFromSuperview]; 
    [self.view insertSubview:menuViewController.view atIndex:0]; 
    [menuViewController viewDidDisappear:YES]; 
    [loadingViewController viewDidAppear:YES]; 
} 
else 
{ 
    if (self.loadingViewController == nil) 
    { 
     LoadingViewController *loadingController = 
     [[LoadingViewController alloc] initWithNibName:@"LoadingView" 
               bundle:nil]; 
     self.loadingViewController = loadingController; 
     [loadingController release]; 
    } 

    [UIView setAnimationTransition: 
    UIViewAnimationTransitionCurlDown 
          forView:self.view cache:YES]; 

    [menuViewController viewWillAppear:YES]; 
    [loadingViewController viewWillDisappear:YES]; 
    [menuViewController.view removeFromSuperview]; 
    [self.view insertSubview:loadingViewController.view atIndex:0]; 
    [loadingViewController viewDidDisappear:YES]; 
    [menuViewController viewDidAppear:YES];   
} 

[UIView commitAnimations]; 

}

我認爲這是太多的代碼,並且如果我必須切換更多的意見,我必須多寫這樣的代碼,它浪費時間,是不是要做到這一點的最好辦法,所以我寫做同樣的事情

- (void)switchtheviews:(UIViewController*)coming over:(UIViewController*)going {  
[UIView beginAnimations:@"frame" context:nil]; 
[UIView setAnimationDuration:1]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:viewController.view cache:YES]; 
[coming viewWillAppear:YES]; 
[going viewWillDisappear:YES]; 
[going.view removeFromSuperview]; 
[viewController.view insertSubview:coming.view atIndex:0]; 
[going viewDidDisappear:YES]; 
[coming viewDidAppear:YES]; 
[UIView commitAnimations]; 

    } 

這麼一個功能,我怎麼能調用這個函數?如果我使用舊的,我可以用

[[MyAppsAppDelegate App].switchViewController loading2menu]; 

但這種新的,我應該怎麼寫調用新的功能?感謝

+0

你能澄清你想要做什麼嗎?你只是想用另一個視圖替換一個視圖? – 2011-06-14 17:57:08

+0

是的,我只是想通過減少代碼來替換一個視圖。我也想知道如何在代碼中調用函數「switchtheviews」,謝謝 – matt 2011-06-14 18:05:04

回答

0

您可以編寫一個C函數是這樣的:

void switchtheviews(UIViewController *coming, UIViewController *going) 
{ 
... 
/* there is no self here */ 
... 
} 

而且在任何地方使用它在您的代碼中:

switchtheviews(v1, v2);

我不認爲這是你想要的。你可以編寫一個私有方法來做同樣的事情,並將所有內容封裝在你的類中。嘗試這樣的事情在你的.m文件(這是私人的東西,沒有必要把它放在.h):

// MegaCoolViewController.m 

@interface MegaCoolViewController() 

- (void)_switchtheviews:(UIViewController*)coming over:(UIViewController*)going; 

@end 


@implementation MegaCoolViewController 

#pragma mark - Private 

- (void)_switchtheviews:(UIViewController*)coming over:(UIViewController*)going 
{ 
/* do your thing, use self freely */ 
} 

@end 

正如你可以在其他的答案讀,有更簡單的方法來切換視圖。我只是回答了問題的語法部分。

0

我不認爲你需要使用動畫什麼你在做。它應該只是是這樣的:

[objectName removeFromSuperview]; 
[superViewName addSubview:otherView]; 

要調用你的函數,它應該是:

[object switchTheViews:viewController over:otherViewController];