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
Related
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!) {
}
i have two view controllers, AbcViewController and XyzViewController. Both controllers behave similarly. Each has a "add" button which opens up a AddNewAbcViewController and AddNewXyzViewController respectively.
On AddNewAbcViewController, when the button "submit" is taped, it does it necessary stuff and close, bringing it back to AbcViewController. I am using delegate here where AbcViewController does the closing of AddNewAbcViewController. This works.
Now I want to do the same for XyzViewController and AddNewXyzViewController, but it is not working. When the btnSubmit is called in AddNewXyzViewController, it didn't enter into XyzViewController dimiss method. I have scanned through my codes many times but don't find anything extra not added. I even gave a different dismiss method name in XyzViewController and AddNewXyzViewController but that didn't work either. What did I miss?
here are my snippets for AbcViewController and AddAbcViewController. The codes for Xyz are identical:
class AddNewAbcViewController.h is
#import <Foundation/Foundation.h>
// protocol
#protocol AddNewAbcProtocol <NSObject>
-(void)dismiss;
#end
#interface AddNewAbcViewController : UIViewController<UITextViewDelegate>
#property(nonatomic, weak)id<AddNewAbcProtocol> delegate;
#end
class AddNewAbcViewController.m is
#interface AddNewAbcViewController() <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
...
#end
#implementation AddNewAbcViewController
...
- (IBAction)btnSubmit:(id)sender
{
[self.delegate dismiss];
}
#end
class AbcViewController.h is
#import <Foundation/Foundation.h>
#import "AddNewAbcViewController.h"
#interface AbcViewController : UIViewController<AddNewAbcProtocol, UISplitViewControllerDelegate>
...
#end
class AbcViewController.m is
#implementation AbcViewController
-(void)dismiss
{
NSLog(#"delegated to dismiss()");
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
As everyone explained, basically you forgot a line of code like ".delegate = self".
Here's a handy beginner's intro to delegates.
https://stackoverflow.com/a/4213005/294884
Use if statement to see if delegate works:
if ([self.delegate respondsToSelector:#selector(dismiss)])
{
[self.delegate dismiss];
}
Create AddNewXyzViewController as an instance variable, but not a local variable.
I have the main viewcontrols with 3 levels (3 x UIButtons) . The first one is visible while the other two are hidden.
The second and third levels are going to be visible just when the previous level is completed.
For example in level 1 when i chose the right question level 2 button became visible and so on.
here an example
Is there a simple way to achieve this? I am new to xcode and I just cannot figure out how to connect the continue button to unhide the level.
here the link with the project file.
http://salvonostrato.com//ex/xcode5/TESTlevels2.zip
Any help is appreciated
In ViwController.h i have:
#import <UIKit/UIKit.h>
#import "Levell1ViewController.h"
#interface ViewController : UIViewController {
IBOutlet UIButton *level1;
IBOutlet UIButton *level2;
IBOutlet UIButton *level3;
}
-(IBAction)unhide:(id)sender;
#end
then in ViewControl.m I have implemented
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
-(IBAction)unhide:(id)sender{
level2.hidden = NO;
level3.hidden = NO;
}
}
-(IBAction)hide:(id)sender{
level2.hidden = YES;
level3.hidden = YES;
}
}
In Livell1ViewControl.h i have called a button
#import <UIKit/UIKit.h>
#interface Levell1ViewController : UIViewController
#property (nonatomic, weak) UIButton *nextlevel2;
#end
The result does not have any issues however, it crashes when the load.
here the upgraded project
http://salvonostrato.com//ex/xcode5/TESTlevels2.zip
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";
}
}
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!