IBAction Delegate - ios

I have a problem with IBAction delegate. I create button in xib file by Interface Builder and want to use it in another controller.
Button is clickable and I don't see any error but it don't show Alert or NSLog. If I try to do NSLog in LetterView.m, It's work perfectly fine but not in ViewController. What should i do?
Thanks for help.
What I am doing currently is:
LetterView.h
#import <UIKit/UIKit.h>
#class LettersView;
#protocol ButtonHint <NSObject>
-(void)hintButtonView:(LettersView*)letterView;
#end
#interface LettersView : UIView #property (strong, nonatomic) IBOutlet UIView *lettersView;
- (IBAction)hintButton:(id)sender;
- (IBAction)removeButton:(id)sender;
#property (nonatomic, assign) id <ButtonHint> hintDelegate;
#end
LetterView.m
-(IBAction)hintButton:(id)sender {
[self.hintDelegate hintButtonView:self];
}
ViewController.h
#interface GameViewController : UIViewController <ButtonDisplayChar, ButtonHint>
ViewController.m
-(void)hintButtonView:(LettersView *)letterView {
NSLog(#"DSfdfsadasda");
[self addAlert:#"HINT" andMessage:helpstr andButton:#"Thanks"];
}

You can do in other View controller by adding target of that Button
example : in your xib have button.
Use that button as target in other view controller.
example :
first in view controller
var letterVw : LetterView?
letterVw.myFirstButton.addTarget(self, action: #selector(myClass.pressed(_:)), forControlEvents: .TouchUpInside)
than
func pressed(sender: UIButton!) {
}

Related

confused about delegate pattern

I'm creating a custom UIView subclass (shareView) from a Nib.
I created a custom protocol for shareView like this:
#protocol sharePopupDelegate
#required
-(void)userPublishedContent;
-(void)userAbortedPublish;
#end
#interface shareView : UIView
{
id<sharePopupDelegate> delegate;
}
In my main.storyboard I created a custom UIViewController myViewController with a shareView view instance called "popup" inside.
So now I got
#property (weak, nonatomic) IBOutlet shareView *popup;
I would like now to delegate myViewController from shareView methods i declared, so I did
self.popup.delegate = self;
inside myViewController but the protocols' methods are not being called inside myViewController.
So i correctly see the shareView instance but cannot interface to its delegate.
Can you help me please?
Thanks in advance
Make sure that you have the protocol declared in myViewController.
For example
#interface MyViewController : UIViewCOntroller <sharePopupDelegate>
In this section of the code:
#interface shareView : UIView
{
id<sharePopupDelegate> delegate;
}
You are creating a strong reference to the delegate, which is not what you want most of the time. Change it to this:
#interface shareView : UIView
#property(weak, nonatomic) id<sharePopupDelegate> delegate;
The shareView class itself must have some way to know when a user publishes content. Maybe you have an action linked up to the shareView which calls a method in the shareView class. For examaple:
- (void)publishButtonTapped {
// some code
}
What you want to do is let the delegate know in that method, something like this:
- (void)publishButtonTapped {
// some code
[self.delegate userPublishedContent];
}
Then whatever action the user takes to cancel:
- (void)cancelButtonTapped {
// some code
[self.delegate userAbortedPublish];
}
Hope this helps.

Segmented Control- Change label text in another view

I have 2 views 1) ViewController and 2)ViewController2.
In the first view controller I have some lablels that when I run the segment controller the names change. All I want to do is, in (Viewcontroller2) where I have a label, I want it to be able to change the text as well based on the segmented controller being 0 or 1. Here is what I have.
// ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController{
IBOutlet UISegmentedControl *Lang;
IBOutlet UIButton *Run;
IBOutlet UILabel *bet;
}
-(IBAction)ChangeLang:(id)sender;
#end
Then in the m file i have the following to change the data
// ViewController.m
#import "ViewController.h"
#import "ViewController2.h"
#interface ViewController ()
#end
#implementation ViewController
-(IBAction)ChangeLang :(id)sender {
if (Lang.selectedSegmentIndex==0)
{
bet.text=#"Apple";
Run.titleLabel.text=#"Banana";
//not sure here what code to put in order to make the label in viewcontroller2 change.
}
if (Lang.selectedSegmentIndex==1) {
bet.text=#"ORANGES";
Run.titleLabel.text=#"FRUITS";
//not sure here what code to put in order to make the label in viewcontroller2 change.
}
}
This runs fine... heres what I have in VIEWController 2
// Viewcontroller2.h
#import <UIKit/UIKit.h>
#interface ViewController2 : UIViewController
IBOutlet UILabel *Nextword;
#end
In the M controller i just have this
#import "ViewController2.h"
#import "ViewController.h"
#interface ViewController2 ()
#end
How would I, when I run the segmented controller, make the uilabel text change in the ViewController2? Can anyone help? I dont want the screen to go to viewcontroller automatically, I was able to do that. I want the text to change, and then when i swipe to go there, i want the text in Viewcontroller2 to be according to the if statement based on the segmented controller.
Thank you
You need to have an access to instance of ViewController2 from ViewController.
Use this code inside your ViewController.h file:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController{
IBOutlet UISegmentedControl *Lang;
IBOutlet UIButton *Run;
IBOutlet UILabel *bet;
ViewConstroller2 * VC2;
}
-(IBAction)ChangeLang:(id)sender;
#end
When you're creating instance of ViewController2 store it in VC2 variable.
Then use this code as your UISegmentedControl change event:
-(IBAction)ChangeLang:(id)sender {
if (Lang.selectedSegmentIndex==0)
{
bet.text=#"Apple";
Run.titleLabel.text=#"Banana";
VC2.Nextword= "YOUR_WORD";
}
if (Lang.selectedSegmentIndex==1) {
bet.text=#"ORANGES";
Run.titleLabel.text=#"FRUITS";
VC2.Nextword= "YOUR_WORD";
}
}

Hide a button on a view from an other view iphone sdk

I have 2 Views, the first named "InterfaceController", and second named "LoginController".
On LoginController, I have a button "Connexion", and I want when I click on this one, I'd like the button I called "TEST" be hidden.
So I tried :
Interfacecontroller.h :
#interface InterfaceController : UIViewController {
- (IBAction)TEST:(id)sender;
- (IBAction)LoginSwitch:(id)sender;
}
Logincontroller.h :
#interface LoginController : UIViewController {
}
- (IBAction)Connexion:(id)sender;
#end
Logincontroller.m :
#import "InterfaceController.h"
#import "LoginController.h"
#implementation LoginViewController
- (IBAction)Connexion:(id)sender {
[self dismissModalViewControllerAnimated:YES];
InterfaceController.TEST.hidden = YES; (this is what I want to set)
}
.........
I don't really know how to do this, but I think it's easy.
Somebody can explain me ?
You have to add an outlet of test button in interfaceController.h:
#property (nonatomic,strong) IBOutlet UIButton *TEST;
after that in Interface Builder you have to link this Outlet to the Test Button.
At this point your code works

Trigger method in parent uiviewcontroller from uibutton in uiscrollview

I have a uibutton inside a uiScrollview inside a uiviewcontroller. When I click the button I want to be able to call a method in the uiviewcontroller.
I have the following action on my button that calls a method within my uiscrollview:
[categoryButton addTarget:self action:#selector(categoryButtonWasClicked:) forControlEvents:UIControlEventTouchUpInside];
But this just calls a method within my uiscrollview (which I will still keep). How do I then call a method in the viewcontroller??
Thank you for any help you can provide.
enter code hereTry doing something like this
#import <UIKit/UIKit.h>
#protocol myUIScrollViewProtocol <NSObject>
-(void)buttonWasPressInScrollView:(id)sender;
#end
#interface MyUIScrollView : UIScrollView
#property (weak, nonatomic)id myDelegate;
#end
Create a subclass of UIScrollView and add a delegate then assign your UIViewController as the delegate and implement the "buttonWasPress.." method in it.
Then from the categroryButtonWasClicked method in your uiScrollView:
-(void)categoryButtonWasClicked{
if ([self.myDelegate respondsToSelector:#selector(buttonWasPressInScrollView:)]){
[self.myDelegate buttonWasPressInScrollView:self];
}
...
}
in your viewController's .h add the following
#interface MyViewController : UIViewController <myUIScrollViewProtocol>

Return to mainview from subview

Basically I want to remove my subview when a button is pressed, here is my code, please tell me what should I add.
I have created a view based application and following are the codes:
//My file name is poo1
//This is poo1ViewController.h file
#import <UIKit/UIKit.h>
#interface poo1ViewController : UIViewController
{
IBOutlet UIButton *fl;
}
#property (nonatomic,retain) UIButton *fl;
-(IBAction) ifl:(id)sender;
#end
This is poo1ViewController.m file
#import "poo1ViewController.h"
#implementation poo1ViewController
#synthesize fl;
-(IBAction) ifl:(id) sender {
UIViewController* flipViewController = [[UIViewController alloc] initWithNibName:#"flip" bundle:[NSBundle mainBundle]];
[self.view addSubview:flipViewController.view];
}
Now similarly I have added a UIViewController Subclass called "flip" with xib. And in flip.h I have added the below code
#import <UIKit/UIKit.h>
#interface flip : UIViewController
{
IBOutlet UIButton *bb;
}
#property (nonatomic,retain)UIButton *bb;
-(IBAction)ss:(id)sender;
#end
And in flip.m
#import "flip.h"
#implementation flip
#synthesize bb;
-(IBAction)ss:(id)sender
{
//What do I need to add here to return to previous view when button is pressed.
}
When this bb button is pressed, it should act as back button, how to implement it.
just add [self removeFromSuperview]; to your method ss:
Note however that this does not dealloc the view from the memory. It's just not showing!

Resources