How to Call Method from One Class in Another (Ios)

How to call another method from another class in swift?

otherClass().methodFromOtherClass() is the syntax for swift

If you have to pass a property with an external name it might be:

otherClass().methodFromOtherClass(propertyName: stringName) //example of passing a string

To call the method to do something, if it is returning a result of a method:

let returnValue = otherClass().methodFromOtherClass(propertyName: stringName)

Example of accessing a property or a function

class CarFactory { 
var name = ""
var color = ""
var horsepower = 0
var automaticOption = false

func description() {
println("My \(name) is \(color) and has \(horsepower) horsepowers")
}
}

var myCar = CarFactory()
myFirstCar.name = "Mustang"
myCar.color = "Red"
myCar.horsepower = 200 myCar.automaticOption = true
myFirstCar.description() // $: "My Mustang is Red and has 200 horsepowers and is Automatic"

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks.

Here you just call the method but not a new instance of the class.

class SomeClass {
class func someTypeMethod() {
// type method implementation goes here
}
}

SomeClass.someTypeMethod()

How to call method from one class in another (iOS)

Objective-C:

You have to import the header of the class that contains the method you want to use (ClassYouWantToUse.h) into the class you want to use it at (TargetClass).

Inside the TargetClass.h or TargetClass.m (depending on the scope you want to give it):

#import "ClassYouWantToUse.h"

Then create an instance of the class you want to use inside the target class either as a property like this:

@property (nonatomic,strong) ClassYouWantToUse *classObject;

Or as an instance variable like this:

ClassYouWantToUse *classObject;

Make sure you initialize it! (usually inside ViewDidLoad):

classObject = [[ClassYouWantToUse alloc] init];

Now you can call any public methods from that class like this:

[classObject theClassMethodWithParam:param1 andSecondParam:param2];

Note: The ClassYouWantToUse class must have the methods that you want to make accessible to others by declaring them in the header file:

- (void)theClassMethodWithParam:(UIImage*)someImage andSecondParam:(NSString*)someText;

Otherwise you won't be able to see these methods.


Swift:

Theres really nothing special about it in swift, just adding this as a reference.

In swift you simply create an instance of the class you want to use:

let classObject = ClassYouWantToUse()

And use it directly:

classObject.theClassMethodWithParam(param1, andSecondParam:param2)

The best way to call a function inside another class? (Swift)

There are basically four ways of calling a function from an other class.

  1. Instance of the class. If you have an instance of say class Object, you can call Object's methods over that instance like myObject.buttonTapped().

  2. By delegation: Create a protocol for class A and declare the method in the protocol. Class A must have an instance of the protocol. Call that method in the buttonTapped method like delegate.notifyButtonTapped(). In class B, conform to the protocol and implement the notifyButtonTapped() method.

  3. Notification: Post a notification over NotificationCenter from class A and addObserver to in class B for that notification. Check this link for further information.

  4. Closure/Blocks: In this case closure is not the best solution and they can be fairly complex to fully understand

You can go with the delegation here. Check this link from rw for clarification.

Call class method with argument from another class method

You need to use class function to be able to use it this way.

class func cycleTimer(toggleOn: Bool) {

However, I'm not sure about thread safety.

Objective-C: Syntax to call method from another class

I suspect this line;

NSArray *newarray = [class1 getTags];

Actually, you should use class instance, not class itself.
To use this kind of function call, you should declare getTags as class level function(+ function), not - function

So your code should be look like this;

class1 *class1Instance = [[class1 alloc] init];
NSArray *newarray = [class1Instance getTags];

Calling a Method from one class and used in another class

i think this problem is caused by the scope of "self" on the method "method". Your button has been added into de firstView and not in the secondView. To do what you whant, you will have to pass the scope that you like to add the button. Like the sample given by @trojanfoe.

-(void)method:(UIView *)_view {
UIButton*Touch1= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[Touch1 addTarget:self action:@selector(TouchButton1:) forControlEvents:UIControlEventTouchUpInside];
[Touch1 setFrame:CGRectMake(50,50, 100, 100)];
Touch1.translatesAutoresizingMaskIntoConstraints = YES;
[Touch1 setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
[Touch1 setExclusiveTouch:YES];
[_view addSubview:Touch1];

NSLog(@"hi ");
}

And into your second view you can call:

 ViewController * ViewCon = [[ViewController alloc]init];
[ViewCon method:self.view];

i think thats the problem, i hope that will help you



Related Topics



Leave a reply



Submit