How do I access IBOutlets that have been created in another class?
here is my code...
Class one
.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (retain, readwrite) IBOutlet UIView *myView;
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize myView;
- (void)viewDidLoad {
[super viewDidLoad];
}
#end
Class two
.h
#import <UIKit/UIKit.h>
#interface ViewController2 : UIViewController
- (IBAction)accion:(id)sender;
- (IBAction)btnBack:(id)sender;
#end
.m
#import "ViewController.h"
#import "ViewController2.h"
#interface ViewController2 ()
#end
#implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)accion:(id)sender {
ViewController *a = [ViewController new];
UIView *someView = [a myView];
[someView setBackgroundColor:[UIColor redColor]];
}
- (IBAction)btnBack:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
why not works? no change the backgroundColor when return on my view
I think you try to delegate pattern to set the values Viewcontroller2.h file
Add protocol like this...
#import <UIKit/UIKit.h>
#protocol tutorialDelegate <NSObject> //set protocol
-(void)delegatesDescribedWithDescription;
#end
#interface ViewController2 : UIViewController
#property (weak, nonatomic) id<tutorialDelegate> tutorialDelegate1;
- (IBAction)accion:(id)sender;
- (IBAction)btnBack:(id)sender;
#end
after the declaring protocol in viewcontroller2.m file first synthesize and call method like this..
#import "ViewController.h"
#interface ViewController2 ()
#end
#implementation ViewController2
#synthesize tutorialDelegate1; // synthesize here
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)accion:(id)sender {
ViewController *a = [ViewController new];
UIView *someView = [a myView];
[someView setBackgroundColor:[UIColor redColor]];
}
- (IBAction)btnBack:(id)sender {
// Here we tell delegate to invoke method in parent view.
[self.tutorialDelegate1 delegatesDescribedWithDescription
];
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
and finally we implement this method in view controller.m file but first set delegate in viewcotroller.h file like this...
#import <UIKit/UIKit.h>
#import "ViewController2.h" //import here
#interface ViewController : UIViewController <tutorialDelegate> //set delegate
#property (retain, readwrite) IBOutlet UIView *myView;
#end
and viewcontroller.m file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize myView;
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"yourIdentifier"]) {
ViewController2 *detailViewController =
segue.destinationViewController;
// here we set the ViewController to be delegate in
// detailViewController
detailViewController.tutorialDelegate1 = self;
}
}
// ViewController must implement tutorialDelegate's methods
// because we specified that ViewController will conform to
// tutorialDelegate protocol
-(void)delegatesDescribedWithDescription
{ // here your code please
viewTemp.backgroundColor =[UIColor redColor];
}
#end
Related
I have Xib UIView which is being displayed by my ViewController. The Xib contains an UILabel and an UIButton. My button coats all over my xib and i'm using it to navigate my SecondViewController and i achieve this by delegate methods.
Here's the thing about my label; because my button is transparent, i can show it beneath the button. What i can't do is to change mylabel's text from ViewController.
I did some search and come across a suggestion like this:
is create another .nib file for the subview and put the subview in
there. Then in that .nib file, make the file owner IOSubview. Property
connections will work just fine there. Then just add the subview to
your IOViewController programatically. Just remember to load the nib
file from bundle first.
link : https://stackoverflow.com/a/20294118/1450201
But it doesn't make sense to me because the reason i created the xib at first is to use it more than once. I believe solution to this problem could be much simpler. But how??
This is what my xib looks like:
And here is a github repo link and my code:
https://github.com/TimurAykutYildirim/demoView
ViewController.h
#import <UIKit/UIKit.h>
#import "Mini.h"
#interface ViewController : UIViewController <SelectionProtocol>
#property (weak, nonatomic) IBOutlet Mini *miniView;
#property (weak, nonatomic) IBOutlet UILabel *miniLabel;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.miniView.delegate = self;
}
-(void) isClicked {
NSString * storyboardName = #"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
[self presentViewController:vc animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Mini.h
#import <UIKit/UIKit.h>
#protocol SelectionProtocol;
#interface Mini : UIView
#property (nonatomic, weak) id<SelectionProtocol> delegate;
- (IBAction)btnClick:(id)sender;
#end
#protocol SelectionProtocol <NSObject>
#required
-(void) isClicked;
#end
Mini.m
#import "Mini.h"
#implementation Mini
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self load];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self load];
}
return self;
}
- (void)load {
UIView *view = [[[NSBundle bundleForClass:[self class]] loadNibNamed:#"Mini" owner:self options:nil] firstObject];
[self addSubview:view];
view.frame = self.bounds;
// ui component properties will be set here
}
- (IBAction)btnClick:(id)sender {
if ([self.delegate conformsToProtocol:#protocol(SelectionProtocol)]) {
[self.delegate isClicked];
}
}
#end
Update your Mini.h to add label outlet to it.
Mini.h
#import <UIKit/UIKit.h>
#protocol SelectionProtocol;
#interface Mini : UIView
#property (nonatomic, weak) id<SelectionProtocol> delegate;
#property (weak, nonatomic) IBOutlet UILabel *miniLabel;
- (IBAction)btnClick:(id)sender;
#end
#protocol SelectionProtocol <NSObject>
#required
-(void) isClicked;
#end
and in ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.miniView.delegate = self;
self.miniView.miniLabel.text = //set whatever value
}
I just started learning objective-c, i was breaking my head why this below simple code for protocols and delegates is not working, please clarify me why this is delegate method is not getting called. Thanks in advance.
ViewController.h
#import <UIKit/UIKit.h>
#include "secondViewController.h"
#interface ViewController : UIViewController<ConverterDelegate>
#property (strong,nonatomic) secondViewController *secondview;
#property (strong, nonatomic) IBOutlet UILabel *msgtext;
#property (strong, nonatomic) IBOutlet UIButton *converbutton;
#end
ViewController.m
#import "ViewController.h"
#import "secondViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize msgtext,converbutton,secondview;
- (void)viewDidLoad
{
[super viewDidLoad];
self.secondview=[[secondViewController alloc]init];
secondview.delegate=self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)convertToFahrenheit:(int)celsius{
float fah = ((1.8)*celsius)+32;
msgtext.text = [NSString stringWithFormat:#"the %i deg Celsius equal to %.2f Fah",celsius,fah];
}
#end
SecondViewController.h
#import <UIKit/UIKit.h>
#protocol ConverterDelegate <NSObject>
#required
-(void)convertToFahrenheit:(int)celsius;
#end
#interface secondViewController : UIViewController
#property (nonatomic, assign) id <ConverterDelegate> delegate;
#property (strong, nonatomic) IBOutlet UITextField *textfield;
- (IBAction)convert:(id)sender;
#end
secondViewController.m
#import "secondViewController.h"
#interface secondViewController ()
#end
#implementation secondViewController
#synthesize textfield,delegate;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (IBAction)convert:(id)sender {
[delegate convertToFahrenheit:[textfield.text intValue]];
//[self dismissViewControllerAnimated:YES completion:nil];
}
#end
I was totally confused why my delegate method is not getting called. I a newbie to Objective c please help me.
I hope following code will fulfil your requirements.
ViewController.h
#import <UIKit/UIKit.h>
#import "Calculation.h"
#interface ViewController : UIViewController<CalculationDelegate>
#property (weak, nonatomic) IBOutlet UILabel *msgtext;
#property (weak,nonatomic) IBOutlet UITextField * inputField;
#property (weak, nonatomic) IBOutlet UIButton *converbutton;
-(IBAction)convertMetod:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize msgtext,converbutton,inputField;
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(IBAction)convertMetod:(id)sender
{
Calculation * CalculationObj = [[Calculation alloc] init];
CalculationObj.delegate = self;
[CalculationObj convertCelToFaher:[inputField.text intValue]];
}
-(void)showTheAns:(NSString *)ans
{
NSLog(#"delegate method called");
msgtext.text = ans;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Calculation.h
#import <Foundation/Foundation.h>
#protocol CalculationDelegate <NSObject>
#required
-(void) showTheAns:(NSString *) ans;
#end
#interface Calculation : NSObject
#property (strong,nonatomic) id <CalculationDelegate> delegate;
-(void)convertCelToFaher:(int)celsius;
#end
Calculation.m
#import "Calculation.h"
#implementation Calculation
#synthesize delegate;
-(void)convertCelToFaher:(int)celsius
{
float fah = ((1.8)*celsius)+32;
[delegate showTheAns:[NSString stringWithFormat:#"the %i deg Celsius equal to %.2f Fah",celsius,fah]];
}
#end
i have problem with UIButton cancel of tableviewcontroller, when I do test, and I click the cancel button to call the action, he did not call the delegate, not work, follow code.
StalkViewController.
#import <UIKit/UIKit.h>
#import "FMDBDataAccess.h"
#import "EditStalkViewController.h"
#import "NewStalkViewController.h"
#interface StalkViewController : UITableViewController<NewStalkViewControllerDelegate>
#property (nonatomic,strong) NSMutableArray *stalks;
-(void)populateStalk;
#end
StalkViewController.m
-(void) addNewStalkViewControllerDidCancel:(NewStalkViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"addStalk"])
{
UINavigationController *navigationController = segue.destinationViewController;
NewStalkViewController *addNewStalkViewController = (NewStalkViewController *)[[navigationController viewControllers] objectAtIndex:0];
addNewStalkViewController.delegate = self;
}
}
NewStalkViewController.h
#import "Stalk.h"
#import "Utility.h"
#class NewStalkViewController;
#protocol NewStalkViewControllerDelegate<NSObject>
-(void) addNewStalkViewControllerDidCancel:(NewStalkViewController *)controller;
#end
#interface NewStalkViewController : UITableViewController<FBFriendPickerDelegate, UITextFieldDelegate>
{
}
#property (nonatomic,weak) id<NewStalkViewControllerDelegate> delegate;
- (IBAction)cancel:(id)sender;
#end
NewStalkViewController.m
#import "NewStalkViewController.h"
#implementation NewStalkViewController
#synthesize delegate;
-(IBAction)cancel:(id)sender
{
[self.delegate addNewStalkViewControllerDidCancel:self];
}
#end
help me!!
How to show an UIIimage by clicking on an UIButton inside another UIViewController? I would like to add to the same UIButton the command to add an image to the SecondViewController. Excused my poor question.
myProtocol.h
#import <Foundation/Foundation.h>
#protocol myProtocol <NSObject>
-(UIImage *)transferImage;
#end
ViewController.h
#import "SecondClass.h"
#interface ViewController : UIViewController<myProtocol, UINavigationControllerDelegate>{
UIView *view;
}
#property (nonatomic,retain) UIImageView *imageView;
- (IBAction)sendImage:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#import "SecondViewController.h"
#import "myProtocol.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad{
[super viewDidLoad];
_imageView = [[UIImageView alloc]initWithImage:
[UIImage imageNamed:#"VoodooVibe#2x.png"]];
[view addSubview:_imageView];
NSLog(#"I am in VC.m");
}
-(UIImage *)transferImage{
NSLog(#"I am in transferImage");
return _imageView.image;
}
- (IBAction)sendImage:(id)sender {
SecondViewController *secClass = [[SecondViewController alloc]init];
secClass.delegate=self;
[secClass callTransfer];
NSLog(#"I am in sender");
[self.navigationController pushViewController:secClass animated:YES];
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
SecondViewController.h
#import <UIKit/UIKit.h>
#import "myProtocol.h"
#import "ViewController.h"
#interface SecondViewController :UIViewController
<myProtocol,UINavigationControllerDelegate> {
UIView *secondView;
IBOutlet UIImageView *myImage;
id <myProtocol> myDelegate;
}
#property (nonatomic,retain) UIImageView *myImage;
#property(nonatomic,assign) id delegate;
-(void)callTransfer;
#end
SecondViewController.m
#import "SecondViewController.h"
#import "ViewController.h"
#import "myProtocol.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
#synthesize delegate,myImage;
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view.
[secondView addSubview:myImage];
}
-(void)callTransfer{
myImage.image=[delegate performSelector:#selector(transferImage)];
myImage.image=[UIImage imageNamed:#"VoodooVibe#2x.png"];
NSLog(#"%#",myImage.image);
NSLog(#"I am in call transfer");
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Delegates should normally be used, if you you have a class included inside your UIViewController which will do something and notify you with the delegate method when the specific process has finished. But in this case you would set an UIImage two times. Once by your delegate and a second time by setting an UIImage programmatically.
You shouldn't do anything like calling a method for initializing the UIImage of the second UIViewController from outside. Just call everything inside viewDidLoad and you don't have to care about it, because the UIViewController handles it itself.
You just have to insert an UIImageView inside your SecondViewController and connect it to your header file. then you can access it inside the m. file. I had the problem that I first used a jpg instead of a png, but after changing the suffix everything worked fine.
ViewController.m
- (IBAction)sendImage:(id)sender {
SecondViewController *secClass = [[SecondViewController alloc] init];
[secClass setImageName:#"pic.png"];
[self.navigationController pushViewController:secClass
animated:YES];
}
SecondViewController.h
#interface SecondViewController : UIViewController
#property (strong, nonatomic)NSString *imageName;
#property (weak, nonatomic) IBOutlet UIImageView *myImage;
#end
SecondViewController.m (There are just these two lines)
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[_myImage setImage:[UIImage imageNamed:_imageName]];
}
I'm trying to figure out delegates because I really need them for a project I'm working on, but for the life of me, I can't figure them out. No matter how much I tweak the code, nothing works
ViewController.h:
#import <UIKit/UIKit.h>
#class ViewController;
#protocol testDelegate
-(void)sayHi;
#end
#interface ViewController : UIViewController
- (IBAction)button:(id)sender;
#property (weak, nonatomic)id <testDelegate> delegate;
#end
ViewController.m:
#import "ViewController.h"
#import "DelegateController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
DelegateController *dc = [[DelegateController alloc] init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)button:(id)sender {
[self.delegate sayHi];
}
#end
DelegateController.h:
#import "ViewController.h"
#interface DelegateController : UIViewController <testDelegate>
#end
DelegateController.m:
#import "DelegateController.h"
#interface DelegateController ()
#end
#implementation DelegateController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
NSLog(#"init");
ViewController *vc = [[ViewController alloc] init];
[vc setDelegate:self];
}
return self;
}
-(void)viewDidLoad
{
[super viewDidLoad];
}
-(void)sayHi
{
NSLog(#"hi");
}
#end
The - (IBAction)button:(id)sender method is connect to a button, but when clicked I get no output. What am I doing wrong?
ViewController.h:
#import <UIKit/UIKit.h>
#protocol testDelegate
-(void)sayHi;
#end
#interface ViewController : UIViewController
#end
ViewController.m:
#import "ViewController.h"
#import "DelegateController.h"
#interface ViewController () <testDelegate>
#end
#implementation ViewController
- (IBAction)pushNewViewController:(id)sender
{
DelegateController *dc = [[DelegateController alloc] init];
dc.delegate = self;
[self.navigationController pushViewController:dc animated:YES];
}
- (void)sayHi
{
NSLog(#"It works!");
}
#end
DelegateController.h:
#import "ViewController.h"
#interface DelegateController : UIViewController
#property (weak, nonatomic) id<testDelegate> delegate;
#end
DelegateController.m:
#import "DelegateController.h"
#implementation DelegateController
- (IBAction)button:(id)sender
{
if([_delegate respondsToSelector:#selector(sayHi)]) {
[_delegate performSelector:#selector(sayHi)];
}
}
#end
Try this:
ViewController.h
#import <UIKit/UIKit.h>
#class ViewController;
#protocol testDelegate
-(void)sayHi;
#end
#interface ViewController : UIViewController
- (IBAction)button:(id)sender;
#property (weak, nonatomic)id <testDelegate> delegate;
#end
ViewController.m
#import "ViewController.h"
#import "DelegateController.h"
#interface ViewController ()
#property (nonatomic, strong) DelegateController *dc;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_dc = [[DelegateController alloc] init];
[self setDelegate:_dc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)button:(id)sender
{
[self.delegate sayHi];
}
#end
DelegateController.h
#interface DelegateController : UIViewController <testDelegate>
#end
DelegateController.m
#import "DelegateController.h"
#interface DelegateController ()
#end
#implementation DelegateController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
NSLog(#"init");
}
return self;
}
-(void)viewDidLoad
{
[super viewDidLoad];
}
-(void)sayHi
{
NSLog(#"hi");
}
#end
Your problem is that ViewController creates DelegateController, then in DelegateController you're creating a new, different, instance of ViewController, and setting yourself as the delegate of that instance. Normally, the way you set a delegate is that DelegateController would create an instance of ViewController and set itself as the delegate. This assumes that DelegateController is created first, but exactly how to do this depends on your app structure, and how you move from controller to controller.
Replace you button Click method with following
- (IBAction)button:(id)sender {
// Check whether your Delegate method is implemeted or not
if([delegate respondsToSelector:#selector(sayHi)])
{
// Call Delegate Method
[delegate performSelector:#selector(sayHi)];
}
}