好吧……这似乎很荒谬,但我正在回答自己:P
我设法让它工作(终于!),我发现我正在以一种糟糕的方式管理视图控制器,所以,我改变了协调控制器中的一些代码:
首先,我没有一个带有 NIB 和其他东西的“真正的”mainViewController……
旧初始化
- (void) initialize{
C6Log(@"");
[self checkDevice];
_mainVC = [C6MainViewController initWithDevice:device];
_activeVC = _mainVC;
[self checkLanguage];
[self chooseFirstView];
}
新初始化
- (void) initialize{
C6Log(@"");
[self checkDevice];
[self checkLanguage];
[self chooseFirstView];
}
检查设备验证它是 iPhone 还是 iPad,这样我就可以选择正确的 NIB。
检查语言检查 [NSUserDefaults standardUserDefaults] 的语言
最后,我调用chooseFirstView:
老选择第一个查看
-(void) chooseFirstView{
// If a language was not setted, go to language settings view
if (!language) {
C6Log(@"Going to Language Settings");
C6LanguageSettingsViewController *languageVC = [C6LanguageSettingsViewController initWithDevice:device];
[_mainVC.view addSubview:languageVC.view];
}
else {
C6Log(@"Going to User Settings", language);
C6AccountSettingsViewController *accountVC = [C6AccountSettingsViewController initWithDevice:device];
[_mainVC.view addSubview:accountVC.view];
}
}
新选择第一视图
-(void) chooseFirstView{
// If a language was not setted, go to language settings view
_activeVC = [[UIViewController alloc] init];
UIImage *bgImage = [UIImage imageNamed:@"bg.png"];
UIImageView *bgView = [[UIImageView alloc] initWithImage:bgImage];
[_activeVC.view addSubview:bgView];
if (!language) {
C6Log(@"Going to Language Settings");
languageVC = [C6LanguageSettingsViewController initWithDevice:device];
[_activeVC.view addSubview:languageVC.view];
}
else {
C6Log(@"Going to User Settings", language);
accountVC = [C6AccountSettingsViewController initWithDevice:device];
[_activeVC.view addSubview:accountVC.view];
}
}
最大的变化是我何时以及如何启动 _activeVC…以及 _languageVC 和 _accountVC 现在都是全局变量这一事实。
好吧,在进行此更改之后,NIB 按钮将调用两个 IBAction 方法:它是文件的所有者和协调控制器。
使用这种模式的另一件大事是如何在不消耗 iOS 设备内存的情况下从一个视图更改为另一个视图……以下是我在协调控制器中执行此操作的方法:
- (IBAction) requestViewChangeByObject:(id)object {
int buttonTag = [object tag]; // dividend
int viewTag = buttonTag / divisor; // quotient
int actionTag = buttonTag - (divisor * viewTag); // remainder
C6Log(@"ViewTag: %d", viewTag);
switch (viewTag) {
case LanguageTags:{
C6Log(@"LanguageTags - button %d", actionTag);
accountVC = [C6AccountSettingsViewController initWithDevice:device];
UIView *fromView = languageVC.view;
UIView *toView = accountVC.view;
[self switchFrom:fromView To:toView usingAnimation:AnimationPushFromRigh];
}
break;
case AccountTags:{
C6Log(@"AccountTags - button %d", actionTag);
switch (actionTag) {
case 0:{
C6Log(@"Go back");
languageVC = [C6LanguageSettingsViewController initWithDevice:device];
UIView *fromView = accountVC.view;
UIView *toView = languageVC.view;
[self switchFrom:fromView To:toView usingAnimation:AnimationPushFromLeft];
}
break;
default:
break;
}
}
break;
default:
break;
}
}
在该方法的开始,我做了很多数学运算……我“创建”了一个模式,其中每个 NIB 都应该有以 100 倍数开头的标签……所以,语言从 0 开始,帐户以 100 开头…………
#define divisor 100
#define LanguageTags 0
#define AccountTags 1
然后,我从一种视图更改为另一种视图的方式就在那里:
-(void) switchFrom:(UIView*) fromView To:(UIView*) toView usingAnimation:(int) animation{
C6Log(@"");
/*************** SET ALL DEFAULT TRANSITION SETTINGS ***************/
// Get the current view frame, width and height
CGRect pageFrame = fromView.frame;
CGFloat pageWidth = pageFrame.size.width;
// Create the animation
[UIView beginAnimations:nil context:nil];
// Create the delegate, so the "fromView" is removed after the transition
[UIView setAnimationDelegate: fromView];
[UIView setAnimationDidStopSelector:@selector(removeFromSuperview)];
// Set the transition duration
[UIView setAnimationDuration: 0.4];
// Add the "toView" as subview of "fromView" superview
[fromView.superview addSubview:toView];
switch (animation) {
case AnimationPushFromRigh:{
// Position the "toView" to the right corner of the page
toView.frame = CGRectOffset(pageFrame, pageWidth,0);
// Animate the "fromView" to the left corner of the page
fromView.frame = CGRectOffset(pageFrame, -pageWidth,0);
// Animate the "toView" to the center of the page
toView.frame = pageFrame;
// Animate the "fromView" alpha
fromView.alpha = 0;
// Set and animate the "toView" alpha
toView.alpha = 0;
toView.alpha = 1;
// Commit the animation
[UIView commitAnimations];
}
break;
case AnimationPushFromLeft:{
// Position the "toView" to the left corner of the page
toView.frame = CGRectOffset(pageFrame, -pageWidth,0);
// Animate the "fromView" to the right corner of the page
fromView.frame = CGRectOffset(pageFrame, pageWidth,0);
// Animate the "toView" to the center of the page
toView.frame = pageFrame;
// Animate the "fromView" alpha
fromView.alpha = 0;
// Set and animate the "toView" alpha
toView.alpha = 0;
toView.alpha = 1;
// Commit the animation
[UIView commitAnimations];
}
break;
default:
break;
}
}
我真的希望这对尝试使用这种协调控制器模式的人有所帮助:P