Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何编写objective-c函数_Objective C_Uiviewcontroller_Switch Statement - Fatal编程技术网

如何编写objective-c函数

如何编写objective-c函数,objective-c,uiviewcontroller,switch-statement,Objective C,Uiviewcontroller,Switch Statement,我使用以下代码切换视图 - (void)loading2menu{ [UIView beginAnimations:@"Loading2Menu" context:nil]; [UIView setAnimationDuration:1.25]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; if (self.menuViewController.view.superview == nil) { if (self.

我使用以下代码切换视图

- (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];

但是这个新函数,调用这个新函数我应该写些什么呢?谢谢你,我认为你不需要用动画来做你正在做的事情。应该是这样的:

[objectName removeFromSuperview];
[superViewName addSubview:otherView];
要调用函数,它应该是:

[object switchTheViews:viewController over:otherViewController];

您可以这样编写一个C函数:

void switchtheviews(UIViewController *coming, UIViewController *going)
{
...
/* there is no self here */
...
}
并在代码中的任何位置使用它:

切换视图(v1、v2)

我认为这不是你想要的。您可以编写一个私有方法来做同样的事情,并将所有内容封装在类中。在您的
.m
文件上尝试类似的内容(这是私人内容,无需将其放在
.h
上):


正如您在其他答案中所看到的,有更简单的方法来切换视图。我只是回答问题的语法部分。

你能澄清一下你想做什么吗?您只是想用另一个视图替换一个视图吗?是的,我只是想通过编写更少的代码来用另一个视图替换一个视图。我还想知道如何在代码中调用函数“switchtheviews”,谢谢
// 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