如何一次又一次地调用方法时,如何声明,初始化整数变量并增加整数变量的值?

时间:2017-11-06 07:01:11

标签: ios objective-c iphone variables increment

考虑我想要一个名为 page 的整数变量,并且第一次将其值设为1.

请参阅sampleViewcontroller.m文件中的示例代码,

// sampleViewcontroller.m 

        - (void)viewDidLoad {

        [self method1];
        }

        -(void) method1
        {

        // Here I am calling one API

        [self reload];

        }

        -(void)reload
        {

        // Here I want to use that page value, whenever this method is called, that value should be incremented by one.
       // and that page value I am passing as a parameter to another method, see below

         MyWebservices *webservices=[MyWebservices sharedInstance];

                [webservices getNextPageURL:_nextPageUrl pageValue:page callbackHandler:^(NSError *error,id json,NSString* msg) {

          // some code

             }

        }

在重载方法中我想使用那个页面值,并希望通过以下方法作为参数,

[webservices getNextPageURL:_nextPageUrl pageValue:page callbackHandler:^(NSError *error,id json,NSString* msg)

用Webservices.m文件编写的方法定义,每次调用reload方法时调用。在这里,我想每次都获取该页面值,当它增加并使用该页面进行下一次api调用时。

// Webservices.m file

        -(void)getNextPageURL:(NSString*)url pageValue:(NSInteger)page callbackHandler:(callbackHandler)block{
            _userDefaults=[NSUserDefaults standardUserDefaults];

            // I want to print this page value (which is incrementing every time)
            NSLog(@"Page value is : %@",page); // incremented value
            NSString *urll=[NSString stringWithFormat:@"%@&api_key=%@&ip=%@&token=%@&page=%@",url,API_KEY,IP,[_userDefaults objectForKey:@"token"],page];

            [self httpResponseGET:urll parameter:@"" callbackHandler:^(NSError *error,id json,NSString* msg) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    block(error,json,msg);
                });

            }];

        }

所以我想将重新加载方法中的 page 值传递给下面的方法,因此每次都会增加值。

-(void)getNextPageURL:(NSString*)url pageValue:(NSInteger)page callbackHandler:(callbackHandler)block

3 个答案:

答案 0 :(得分:0)

只需在您的班级中持有私人财产,例如

@interface SampleViewcontroller() 

@property (nonatomic) NSInteger page;

@end 

@implementation

- (void)viewDidLoad {
  self.page = 0;
  [self method1];  
}

-(void) method1
{

  // Here I am calling one API

  [self reload];

}

-(void)reload
{

   // Here I want to use that page value, whenever this method is called, that value should be incremented by one.
   // and that page value I am passing as a parameter to another method, see below

   MyWebservices *webservices=[MyWebservices sharedInstance];    
  [webservices getNextPageURL:_nextPageUrl pageValue:self.page callbackHandler:^(NSError *error,id json,NSString* msg) {

      // some code
      if (!error) {
          //If success
          self.page++;
      }
  }

}        
@end

答案 1 :(得分:0)

在.h文件中写下以下内容:

@property (nonatomic) NSInteger page;

现在在.m文件中

- (void)viewDidLoad {

 self.page = 1; // as you initially want page value to be 1. 
[self method1];  
}

-(void) method1
 {

 // Here I am calling one API
 self.page = page + 1; // as you want to increment the value of page when before function is called
 [self reload];
 }

 -(void)reload
    {

    // Here I want to use that page value, whenever this method is called, that value should be incremented by one.
   // and that page value I am passing as a parameter to another method, see below

     MyWebservices *webservices=[MyWebservices sharedInstance];

            [webservices getNextPageURL:_nextPageUrl pageValue:self.page callbackHandler:^(NSError *error,id json,NSString* msg) {

      // some code

         }

    }

答案 2 :(得分:0)

    You don't need to add a property, just add a private variable using anyone from the following ways:

    1. In .h file:

    @interface ViewController : UIViewController {

        NSInteger page;
    }

    OR 
    2. In .m file:


    @interface ViewController ()
    @end

    @implementation ViewController

    NSInteger page;

    // Now use the page variable in .m file:

    - (void)viewDidLoad {

        [self method1];
        }

        -(void) method1
        {

        // Here I am calling one API

        [self reload];

        }

        -(void)reload
        {

        // Here I want to use that page value, whenever this method is called, that value should be incremented by one.
       // and that page value I am passing as a parameter to another method, see below

         MyWebservices *webservices=[MyWebservices sharedInstance];

                [webservices getNextPageURL:_nextPageUrl pageValue:page callbackHandler:^(NSError *error,id json,NSString* msg) {

          // some code

             page ++;

             }

        }