问题描述
|
也许我正在尝试以错误的方式来处理这个问题...
在主视图控制器中,我有三个按钮。
每个按钮都会导致加载不同的UIViewController及其笔尖。
我使用一种方法来处理按钮触摸:
-(IBAction)touched_menu_button:(id)sender
{
SEL selector;
UIButton *button = (UIButton *)sender;
switch (button.tag)
{
case 0:
selector = @selector(ShowA:finished:context:);
break;
case 1:
selector = @selector(ShowB:finished:context:);
break;
case 2:
selector = @selector(ShowC:finished:context:);
break;
default:
selector = @selector(ShowA:finished:context:);
break;
}
[self FadeOut:selector];
}
这里发生的是在实际显示新视图之前,我正在运行一些动画。我使用选择器调用\“ FadeOut \”,该选择器将调用动画完成后将显示适当视图的方法。这很好。选择器调用的例程如下所示:
-(void)ShowA:(Nsstring *)animationID finished:(NSNumber *)finished context:(void *)context
{
A_ViewController *mvc = [[[A_ViewController alloc] init] autorelease];
mvc.delegate = self;
[self presentModalViewController:mvc animated:FALSE];
}
这也很好。
我想做的就是将其简化为选择器调用的单个方法,并将所有冗余代码放入“ touched_menu_button \”方法中。回调函数将如下所示:
-(void)ShowNewView:(Nsstring *)animationID finished:(NSNumber *)finished context:(void *)context
{
[self presentModalViewController:mvc animated:FALSE];
}
显然,这需要根据我要显示的视图在\“ mvc \”中传递不同的UIViewController子类。这就是我遇到麻烦的地方。也许是我已经连续编程了12个小时,并且精神上被炸了,但是我似乎不知道该怎么做。我尝试将类变量定义为void,void *,id,UIViewController *和UIViewController **。由于某种原因,我似乎无法使其正常运行。
我应该注意,有很多方法都可以使用,但是,当视图关闭并且发生自动释放过程时,它们会遇到麻烦。似乎在我尝试过的所有排列中,都无法将指针的地址传递给UITableView子类。我现在能确定的唯一选择确实很丑。
解决方法
可以将类本身传递,而不是将对象传递给回调。
-(IBAction)touched_menu_button:(id)sender {
Class theClass;
UIButton *button = (UIButton *)sender;
switch (button.tag) {
case 0:
theClass = [A_ViewController class];
break;
case 1:
theClass = [B_ViewController class];
break;
case 2:
theClass = [C_ViewController class];
break;
default:
theClass = [A_ViewController class];
break;
}
[self FadeOut:theClass];
}
FadeOut:
然后使用该类作为动画的上下文信息:
- (void)FadeOut:(Class)theClass {
//...
[UIView beginAnimations:@\"FadeOut\" context:(void*)theClass];
//...
[UIView commitAnimations];
//...
}
如果您将上下文用于其他用途,则可以使用变量,因为它似乎是您试图与视图控制器对象一起使用的变量。回调然后分配给定的类。
-(void)ShowA:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
Class theClass = (Class)context; // or get it from a variable if you don\'t use the context
// If the view controllers all have a shared superclass which declares the delegate property,you can specify that as the type and use dot notation,but otherwise you will need to call setDelegate:
UIViewController *mvc = [[[theClass alloc] init] autorelease];
[mvc setDelegate:self];
[self presentModalViewController:mvc animated:FALSE];
}