如何解决如何编写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];
但是这个新函数,调用新函数应该写些什么呢?
解决方法
您可以编写如下的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
正如您在其他答案中可以看到的那样,有更简单的方法来切换视图。我只是在回答问题的语法部分。
, 我认为您不需要为正在执行的操作使用动画。它应该像这样:
[objectName removeFromSuperview];
[superViewName addSubview:otherView];
要调用您的函数,它应该是:
[object switchTheViews:viewController over:otherViewController];
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。