-4

I am fairly new to the objective C scene. I was writing an app, and I noticed I kept repeating myself, so why not create a method instead? I was trying to create a method with multiple parameters, these parameters are the color and location of a UIButton. So essentially everytime i call this method I will be able to create a UIbutton. How would I go about creating this as its own method?

   //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];

As you can see, I am creating two different buttons, just with a different color and location, so i would rather have a method that allows me to create the buttons instead. Please guide me step by step as to how to create this method. Thank you!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user3413380
  • 75
  • 11
  • 2
    Have you made any effort to create the method? If you don't know enough Objective-C to write a method, may I politely suggest finding a good tutorial on the language. SO is not really the right place to learn the basics. – rmaddy Jul 22 '14 at 16:12
  • possible duplicate of [How do I pass multiple parameters in Objective-C?](http://stackoverflow.com/questions/722651/how-do-i-pass-multiple-parameters-in-objective-c) – Stephen Darlington Jul 22 '14 at 16:17

2 Answers2

0

This is something that I‘ve been meaning to do with my own code, just haven’t gotten around to it.

Something like this should work:

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

  // Your code goes here. 
}

The just call it with the appropriate parameters. If you want to feed in lots of parameters, you might consider using an NSDictonary instead of listing them. e.g.

- (void)displayButton:(NSDictionary *)buttonParameters 

  // Your code goes here. 
}

While I haven’t done it with buttons, I use a similar method to lay out grids for putting objects on the screen. I use it a lot so I put it into its own class.

+ (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;
}

I return an NSDictionary that I use to display the objects.

JScarry
  • 1,507
  • 1
  • 13
  • 25
-1

The first decision I would make is to figure out if I need this within an object or just a function for all components to have access to. If I decide that it will be a method of an object then we have two tasks:

  • After creating the class as new files (+ in lower left corner) in the Xcode environment, we will have .h and .m files. The .h, which will appear as myObj.h and myObj being the name of the object, is where we can declare all the properties and methods of our object. Hence, we add the method here like so:

    myObj.h

     -(returnValue) myMethod: (parameter) 
                      withNextParam: (parameter)
                      withOtherParam: (parameter);
    

The returnValue is whatever the returning data type is (i.e NSString, ect), if you wish to not return anything void is the option to choose. The descriptors for the multiple parameters are created by you, relevancy is key being that you may have 4-5 different parameters and for others to see the code logic through more clearly.

  • Once we have added our method in the .h file, we still have to add in the actual code for this method. The .m file is the place to do so:

    myObj.m

    -(returnValue) myMethod: (parameter) 
                      withNextParam: (parameter)
                      withOtherParam: (parameter){
    //    Enter Code Here...
    }
    

At this point we have our method complete and ready to use. The last part is calling the method.

  • Create an object within your AppDelegate.m file or where ever it is you would be calling the method.

       myObj * firstObject = [[myObj alloc] init];
    

This code will allocate memory for the new object and then initialize to make it usable to manipulate. All objects will take a pointer (*) when being created.

  • We take our new object and tell it to perform the given method:

    [firstObject myMethod: (parameter)
                       withNextParam: (parameter)
                       withOtherParam: (parameter)];
    

    The only way this would change is if we have a returned value in which case:

    (returnValue*) returnedVar = [firstObject myMethod: (parameter)
                       withNextParam: (parameter)
                       withOtherParam: (parameter)];
    
fingaz
  • 241
  • 1
  • 6
  • The OP is obviously new and doesn't know how to create the method. Your answer doesn't really help the user understand how to create a new method to meet their needs. This answer is a vague way of describing how to call an existing method, not about how to write a new method. – rmaddy Jul 22 '14 at 16:26
  • It's not an issue of reputation, it's an issue of posting a useful (or not) answer. Feel free to update your answer to improve it or delete it if you wish. – rmaddy Jul 22 '14 at 17:05
  • @rmaddy ok, ambition accepted, I tried to give another perspective to answer. But does this seem relevant? – fingaz Jul 23 '14 at 21:03