I have a button action function in a controller named as "Remotecontroller". this is the method:
-(IBAction)startDiscover:(id)sender{.....}
I have another controller named as "iptvViewcontroller". I need to call the above method in this "iptvViewcontroller" when a button click. this is my button click function:
btnRefresh3 = [UIButton buttonWithType:UIButtonTypeCustom];
btnRefresh3.frame = CGRectMake(0, 0, 25, 20);
[btnRefresh3 addTarget:self action:#selector(startDiscover:)
[arrLeftBarItems addObject:barButtonItem3];
Please somebody help me to make it work.
By creating and using custom delegate , you can achieve that functionality.
An Objective-C delegate is an object that has been assigned to the delegate property another object.
See below links for details and How to create and use custom delegate.
1.How do I create delegates in Objective-C?
2.How to use custom delegates in Objective-C
In Remotecontroller.m, set NotificationCenter like this in your viewDidLoad :
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(startDiscover) name:#"discover" object:nil];
In Remotecontroller.m
-(void)startDiscover
{
/////Your Code
}
In iptvViewcontroller.m
[btnRefresh3 addTarget:self action:#selector(startDiscoverFromRemoteController);
-(void)startDiscoverFromRemoteController
{
[[NSNotificationCenter defaultCenter]postNotificationName:#"discover" object:nil];
}
To call one method to another class you have to write your method in Remotecontroller.h file
-(IBAction)startDiscover:(id)sender;
then your remotecontroller class import in iptvViewcontroller class in .h file like this
#import "Remotecontroller.h"
now to use that method you have to create and alloc init object of remotecontroller class in iptvViewcontroller class in iptvViewcontroller.h file viewdidload() method
Remotecontroller *remote;
in iptvViewcontroller.m file
remote = [Remotecontroller alloc]init];
now you can use your method throughout the class
[remote startDiscover:parameter];
Related
I have a class name AccountRecoveryViewController. Here in UITableView's "customcell" which is called AccountRecoveryExpandedCell in - (void)awakeFromNib method I simply add a customView called RIPasscodeView. And set all necessary parameter.
Like this:
- (void)awakeFromNib
{
RIPasscodeView *passcodeView = [[RIPasscodeView alloc] initWithFrame:CGRectMake (32, 144, 278, 35)];
passcodeView.innerSpaceValue = 20;
passcodeView.placeHolder = #"-";
passcodeView.borderColor = [UIColor colorWithRed:250.0f/255.0f green:110.0f/255.0f blue:40.0f/255.0f alpha:1.0f];
passcodeView.delegate = self;
[self addSubview:passcodeView];
}
In RIPasscodeView I set the UITextFields delegate methods (textFieldShouldReturn or shouldChangeCharactersInRange) and it's works fine.
But now I want to access those UITextFields delegate methods from my AccountRecoveryViewController. Because I have to change the position of my tableview (up a little bit) by tap gesture, but the UITextFields delegate methods which is not available in this AccountRecoveryViewController class.
How can I do that? If any one have any suggestion or query please knock me.
Thanks a lot in advance.
You have several options to do this.
Create your own Protocol, and handle on action from inside the RIPasscodeView to AccountRecoveryViewController.
Assign the UITextField delegate to your parent controller, by passing the parent controller reference.
Use KVO.
Use NSNotificationCentre to notify your parent controller.
Cheers.
i have UICollectionViewController with custom UICollectionViewCell which contains child UICollectionView and custom childUICollectionViewCell.
UICollectionViewController
UICollectionViewCell (parentCell)
UILable
UICollectionView
UICollectionViewCell(childCell)
UILable (valueLable)
UIButton (Submit)
UICollectionViewCell(childCell)
UILable (valueLable)
UIButton (Submit)
i want to send text of "valueLable" on click "Submit" button to new UIViewController.
Please help me....
Create a protocol SubmissionDelegateProtocol with a method -(void) submitWithValue:(NSString*)text;
Your UICollectionViewController implements (conforms to) the protocol.
It is supposed to provide this methods for the childViews to call it.
On creating the collection views and further collection and its collection views you will have to pass down a -let's say- sumbmitDelegate property (or init parameter) that is holding a reference to the UICollectionViewController which implements the protocol.
id <SubmissionDelegateProtocol> submissionDelegate;
When you work with storyboards, then the method prepareForSegue: is the appropriate place for handing further information down to subsequent view controllers. But if both collection views use the same ViewContoller as their dataSource, as your tree suggests, then it should be even easier.
So that in the end each of the cells holds a (weak) reference to the view conotroller.
Then, upon button is pressed action, you call the delegate method
[submissionDelegate submitWithValue:[valueLable text]];
That's for the delegation pattern.
Alternatively you could work with notifications.
Try NSNotification Center
In Child View Controller, postNotifictaion
and in UIViewController listen to the same notification.
You can also pass data using NSNotificationCenter.
- (void) submitButtonClicked
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"dataFromChildToParent" object:nil userInfo: valueLable.text];
}
and In UIViewControllers ViewDidLoad Method
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(loadRegistrationNotification:) name:#"dataFromChildToParent" object:nil];
and Implement the selector
- (void) loadRegistrationNotification:(NSNotification *)noti {
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"dataFromChildToParent" object:nil];
NSString *valueLabelText = [noti userInfo];
NSLog(#"Notification data %#", valueLabelText);
}
For Detailed Explanation about NSNotificationCenter see this Tutorial
I have several buttons in one class and their actions are separate.
I created the instance of that class in another class, and I have the UIButton array in the 2nd class. I want to call each button's action programmatically. Is there any way to do this in iOS?
UIButton has a method to invoke the targets/selectors that are linked to a certain control event:
[button sendActionsForControlEvents:UIControlEventTouchUpInside];
2022 Swift syntax:
someButton.sendActions(for: .primaryActionTriggered)
For Swift 3.0/4.0/5.0:
button.sendActions(for: .touchUpInside)
You can simply call first class's action method as instance method from other classes by passing a UIbutton id
e.g.:
in first class "ClassA"
- (IBAction)classAbuttonAction1:(id)sender;
- (IBAction)classAbuttonAction2:(id)sender;
from another class
UIButton *bClassButton = [[UIButton alloc]init];
ClassA *instance = [[ClassA alloc]init];
[instance classAbuttonAction1:bClassButton];
[instance classAbuttonAction2:bClassButton];
Thanks for your help in advance!
I'm new to OOP, so this problem may be really basic, but I've searched for hours and still cannot find a good solution.
I'm using Cocos2d and Box2d in the project. In my GameLayer.mm file, I have a label to show current score. And there's is a custom sprite derived from CCSprite.
Now, I wanna increment current score from my custom sprite class when the property of sprite "isDead" is changed to true. As follows:
- (void) setIsDead
{
isDead = 1;
// then increment score
}
My question is how I can increment score from this subclass? I cannot access the instance or instance method of GameLayer.mm from this subclass. I tried to change the function of incrementing score from instance method to class method, and make score as a global instance, but I got duplicate error later.
Thanks for any advice!
Here is another approach I like: delegates.
First, go to your custom CCSprite header and create a new protocol. Basically, add this:
#protocol MyCustomDelegate
-(void)spriteGotKilled:(CCSprite*)sprite;
#end
Next, you need to modify your custom CCSprite to store its delegate. Your interface would look like this:
#interface MySprite {
id delegate;
}
#property (retain,nonatomic) id delegate;
Now go to GameLayer.h and make it implement the protocol:
#interface GameLayer : CCLayer <MyCustomDelegate>
Next implement the protocol method in your layer:
-(void)spriteGotKilled:(CCSprite*)sprite {
NSLog(#"%# got killed!",sprite);
}
And, finally, go to your setIsDead method:
-(void)setIsDead {
isDead = 1;
// Check if we have a delegate set:
if ([self delegate] != nil) {
// Check if our delegate responds to the method we want to call:
if ([[self delegate]respondsToSelector:#selector(spriteGotKilled:)]) {
// Call the method:
[[self delegate]spriteGotKilled:self];
}
}
}
When you create your sprite, you must set the layer as its delegate. Something like this:
MySprite *sprite = [[MySprite alloc]init];
[sprite setDelegate:self];
Now whenever your sprite dies, spriteGotKilled will be called in your layer.
You could use The Observer Design Pattern here in which an observer listens to an event and performs an action accordingly.
So in your GameLayer.mm add the observer in the init function:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveIsDeadNotification:)
name:#"SpriteIsDeadNotification"
object:nil];
and add a function:
- (void) receiveIsDeadNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:#"SpriteIsDeadNotification"])
//update your label here
}
and in your custom sprite ,add the following line in the setIsDead method
-(void) setIsDead{
isDead =1;
[[NSNotificationCenter defaultCenter]
postNotificationName:#"SpriteIsDeadNotification"
object:self];
}
also remember to remove the observer in the dealloc of GameLayer.mm
[[NSNotificationCenter defaultCenter] removeObserver:self];
This pattern will reduce coupling in your code as instances of one class are not trying to access the methods of another.
I have a category of a class that creates a button that initializes properly with the following options
[cancelButton addTarget:self action:#selector(cancelReconnect:) forControlEvents:UIControlEventTouchUpInside];
then in the same category.m file I have the method
-(void)cancelReconnect{ NSLog(#"here!"); }
When the button is pressed in the viewcontroller that imports the class which imports the category I get an EXC_BAD_ACCESS and it appears my method/class seems like it was released.
If I put the same cancelReconnect method in the viewcontroller where 'self' would be. It is the same result.
Is my addTarget correct? is my selector method being released? How to solve this?
note the toolBarItems array in my custom class' .h file is strong but I don't know if the button in the array is. Does the View Controller keep a strong pointer to the Class's category method
You are making a common mistake. Your actual method is named cancelReconnect but you tell the button that the selector is named cancelReconnect: (notice the colon).
Change your method to:
- (void)cancelReconnect:(UIButton *)button {
NSLog(#"here!");
}