Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何创建具有多个参数的方法xcode_Ios_Objective C - Fatal编程技术网

Ios 如何创建具有多个参数的方法xcode

Ios 如何创建具有多个参数的方法xcode,ios,objective-c,Ios,Objective C,我对objective C场景还相当陌生。我在写一个应用程序,我注意到我一直在重复我自己,所以为什么不创建一个方法呢?我试图创建一个具有多个参数的方法,这些参数是UIButton的颜色和位置。所以基本上每次我调用这个方法,我都能创建一个UIbutton。我将如何创建它作为自己的方法 //Left Button uploadPhotoButton = [UIButton buttonWithType:UIButtonTypeCustom]; [uploadPhotoButton setIm

我对objective C场景还相当陌生。我在写一个应用程序,我注意到我一直在重复我自己,所以为什么不创建一个方法呢?我试图创建一个具有多个参数的方法,这些参数是UIButton的颜色和位置。所以基本上每次我调用这个方法,我都能创建一个UIbutton。我将如何创建它作为自己的方法

   //Left Button
uploadPhotoButton = [UIButton buttonWithType:UIButtonTypeCustom];

[uploadPhotoButton setImage:[UIImage imageNamed:@"uploadphotoicon"] forState:UIControlStateNormal];

uploadPhotoButton.frame = CGRectMake(0, 0, 80, 80);

uploadPhotoButton.clipsToBounds = YES;

uploadPhotoButton.layer.cornerRadius = 80/2.0f;
uploadPhotoButton.layer.borderColor = [UIColor colorWithRed:.102 green:.737 blue:.612 alpha:1.0].CGColor;
uploadPhotoButton.layer.borderWidth = 2.0f;

[self.view addSubview:uploadPhotoButton];
[uploadPhotoButton setCenter:CGPointMake(78, 139)];
[uploadPhotoButton addTarget:self action:@selector(uploadPhotoButton:) forControlEvents:UIControlEventTouchUpInside];

//Right Button

uploadPhotoButton2 = [UIButton buttonWithType:UIButtonTypeCustom];

[uploadPhotoButton2 setImage:[UIImage imageNamed:@"uploadphotoicon"] forState:UIControlStateNormal];

uploadPhotoButton2.frame = CGRectMake(0, 0, 80, 80);

uploadPhotoButton2.clipsToBounds = YES;

uploadPhotoButton2.layer.cornerRadius = 80/2.0f;
uploadPhotoButton2.layer.borderColor = [UIColor colorWithRed:.749 green:.580 blue:.894 alpha:1.0].CGColor;
uploadPhotoButton2.layer.borderWidth = 2.0f;

[self.view addSubview:uploadPhotoButton2];
[uploadPhotoButton2 setCenter:CGPointMake(242, 139)];
[uploadPhotoButton2 addTarget:self action:@selector(uploadPhotoButton:) forControlEvents:UIControlEventTouchUpInside];
如您所见,我正在创建两个不同的按钮,只是颜色和位置不同,因此我更希望使用一种方法来创建按钮。请逐步指导我如何创建此方法。
谢谢大家!

我要做的第一个决定是弄清楚我是需要在一个对象中使用它,还是只需要一个所有组件都可以访问的函数。如果我决定它将是一个对象的方法,那么我们有两个任务:

在Xcode环境的左下角将类创建为new files+后,我们将有.h和.m文件。.h将显示为myObj.h,myObj是对象的名称,在这里我们可以声明对象的所有属性和方法。因此,我们在此处添加如下方法:

myObj.h

 -(returnValue) myMethod: (parameter) 
                  withNextParam: (parameter)
                  withOtherParam: (parameter);
返回值是返回数据类型的任意值,即NSString,ect,如果您希望不返回任何内容,则可以选择void。 多个参数的描述符由您创建,相关性的关键在于您可能有4-5个不同的参数,其他人可以更清楚地看到代码逻辑

一旦我们在.h文件中添加了我们的方法,我们仍然需要为这个方法添加实际的代码。.m文件是执行此操作的位置:

myObj.m

-(returnValue) myMethod: (parameter) 
                  withNextParam: (parameter)
                  withOtherParam: (parameter){
//    Enter Code Here...
}
现在,我们的方法已经完成,可以使用了。最后一部分是调用该方法

在AppDelegate.m文件中创建一个对象,或者在调用该方法的任何位置创建该对象

   myObj * firstObject = [[myObj alloc] init];
此代码将为新对象分配内存,然后进行初始化以使其可用于操作。所有对象在创建时都将使用指针*

我们获取新对象并告诉它执行给定的方法:

[firstObject myMethod: (parameter)
                   withNextParam: (parameter)
                   withOtherParam: (parameter)];
唯一改变的方法是如果我们有一个返回值,在这种情况下:

(returnValue*) returnedVar = [firstObject myMethod: (parameter)
                   withNextParam: (parameter)
                   withOtherParam: (parameter)];

这是我一直想用自己的代码来做的事情,只是还没来得及去做

像这样的方法应该会奏效:

- (void)displayButton:(NSString *)buttonName 
         withFontSize:(NSString *)fontSize 
            withColor:(UIColor)buttonColor
           atLocation:(CGRect)location { 

  // Your code goes here. 
}
只需使用适当的参数调用它。如果你想输入大量的参数,你可以考虑使用NScript,而不是列出它们。e、 g

- (void)displayButton:(NSDictionary *)buttonParameters 

  // Your code goes here. 
}
虽然我没有使用按钮,但我使用类似的方法来布置网格,以便在屏幕上放置对象。我经常使用它,所以我把它放在自己的类中

+ (NSDictionary *)findFramesWithParentView:(UIView *)parentView
                              numberOfRows:(NSUInteger)numberOfRows
                           numberOfColumns:(NSUInteger)numberOfColumns
                                 pictWidth:(NSUInteger)pictWidth
                                pictHeight:(NSUInteger)pictHeight
                             densityFactor:(NSUInteger)densityFactor
                         itemLocationArray:(NSArray *)itemLocationArray
                        withSwipeDirection:(NSString *)swipeDirection {

    // Calculations go here

    NSDictionary *frames = @{@"pictures": pictFrames, @"finalX": finalX};
    return frames;
}

我返回一个用于显示对象的NSDictionary。

您是否努力创建了该方法?如果您对Objective-C不太了解,无法编写一个方法,我可以礼貌地建议您找一个好的语言教程。因此,这并不是学习基础知识的好地方。OP的可能副本显然是新的,不知道如何创建方法。您的回答并不能真正帮助用户理解如何创建新方法来满足他们的需求。这个答案是描述如何调用现有方法的模糊方式,而不是如何编写新方法。这不是声誉问题,而是发布有用或不有用的答案的问题。如果您愿意,请随时更新您的答案以改进它或删除它。@rmaddy ok,接受雄心壮志,我试图给出另一种观点来回答。但这似乎相关吗?