【问题标题】:iOS Reusing Methods - can this only be done with condition?iOS重用方法 - 这只能在条件下完成吗?
【发布时间】:2012-10-11 20:49:04
【问题描述】:

我正在尝试在我的代码中重用一个方法,如果没有这样的条件,它就可以工作:

-(void) declareWebView

我可以稍后在任何其他方法中调用它:

[self declareWebView];

但是如果我想给它添加一个条件(我认为这就是它的名字),就像这样:

-(void) declareWebView:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration

然后我如何在[self declareWebView]; 中使用它??

如果我执行以下操作,它会给我一个错误:

[self declareWebView];[self declareWebView:]; //Second 添加了 Column。

任何都会很棒!

【问题讨论】:

    标签: objective-c ios methods


    【解决方案1】:

    如果你想创建一个新方法来实现与前一个相同的代码,这是我的解决方案。

    - (void) originalMethod{
        [self originalMethodWithNumber:nil];
    }
    
    
    - (void) originalMethodWithNumber:(NSNumber*) newNumberParam{
        NSNumber *numberValue = newNumberParam;
        if (numberValue == nil) numberValue = [NSNumber numberWithInt:1];//default value.
        // Perform anything here that you were doing. replacing out the new parameter
    }
    

    那么您仍然可以调用旧的“originalMethod”,现在有了一个带参数的新方法。

    【讨论】:

      【解决方案2】:

      您必须为每个参数传递一个值:

      [self declareWebView:UIInterfaceOrientationPortrait duration:1.0];
      

      我建议您阅读一下——The Objective-C Programming Language 是一个很好的入门指南。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-03-30
        • 1970-01-01
        • 1970-01-01
        • 2015-04-13
        • 1970-01-01
        • 1970-01-01
        • 2017-05-02
        相关资源
        最近更新 更多