My Delegate method is not getting Called - ios

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

Related

Accessing user defined delegate via multiple ViewControllers - iOS

I'm using the BLE framework files that came with some hardware, but I'm only able to call the functions in my first view controller. My application has two view controllers and it is my intention to write and read BLE data in both views, using the BLEDelegate.
Everything works as expected in the first view controller, but I get no action from the BLE files in the second view controller.
This is a skeleton of my project.
BLE.h
#import <CoreBluetooth/CoreBluetooth.h>
#protocol BLEDelegate
#optional
-(void) bleDidReceiveData:(unsigned char *) data length:(int) length;
#required
#end
#interface BLE : NSObject;
#property (nonatomic,assign) id <BLEDelegate> delegate;
-(void) write:(NSData *)d;
#end
BLE.m
#import "BLE.h"
#interface BLE ()
#end
#implementation BLE
#synthesize delegate;
-(void) write:(NSData *)d
{
//send BLE packet
NSData *data = [NSData dataWithBytes: &d length: sizeof(d)];
NSLog(#"%#", data);
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import "BLE.h"
#interface ViewController : UIViewController<BLEDelegate>
#property (nonatomic, strong) BLE *ble;
#property (weak, nonatomic) IBOutlet UIButton *btnClicked;
#end
ViewController.m
#import "ViewController.h"
#import "SecondViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize ble;
- (void)viewDidLoad {
[super viewDidLoad];
ble = [[BLE alloc] init];
ble.delegate = self;
}
-(void)bleDidReceiveData:(unsigned char *)data length:(int)length
{
//parse data
}
- (IBAction)btnClicked:(id)sender
{
uint32_t packet = 0xFFFF;
NSData *data = [NSData dataWithBytes: &packet length: sizeof(packet)];
[ble write:data];
}
#end
SecondViewController.h
#import <UIKit/UIKit.h>
#import "BLE.h"
#interface SecondViewController : UIViewController<BLEDelegate>
#property (nonatomic, strong) BLE *ble;
#property (weak, nonatomic) IBOutlet UIButton *btnClicked;
#end
SecondViewController.m
#import "SecondViewController.h"
#import "ViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
#synthesize ble;
- (void)viewDidLoad {
[super viewDidLoad];
ble = [[BLE alloc] init];
ble.delegate = self;
}
-(void)bleDidReceiveData:(unsigned char *)data length:(int)length
{
//parse data
}
- (IBAction)btnClicked:(id)sender
{
uint32_t packet = 0xFFFF;
NSData *data = [NSData dataWithBytes: &packet length: sizeof(packet)];
[ble write:data];
}
#end

Objective c - pass back string value using delegate

I want to pass UITextField *nameText from GreenViewController to UILabel *nameValue in ViewController using delegate.
I made delegate, the onSave method in ViewController is called but the screen is not going back and the name is not passed.
What is the problem?
Here are the header and impelentation files:
GreenViewController.h
#import <UIKit/UIKit.h>
#protocol GreenViewDelegate <NSObject>
-(void)onSave:(NSString*)nameValue;
#end
#interface GreenViewController : UIViewController
#property id<GreenViewDelegate> delegate;
#property (nonatomic) NSString* sliderValuePassed;
#property (weak, nonatomic) IBOutlet UIButton *Next;
#property (weak, nonatomic) IBOutlet UIButton *Save;
#property (weak, nonatomic) IBOutlet UIButton *Back;
#property (weak, nonatomic) IBOutlet UILabel *sliderTextValue;
#property (weak, nonatomic) IBOutlet UITextField *NameText;
- (IBAction)save:(id)sender;
#end
GreenViewController.m
#import "GreenViewController.h"
#import "ViewController.h"
#interface GreenViewController ()
#end
#implementation GreenViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.sliderTextValue.text = self.sliderValuePassed;
}
- (IBAction)back:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)save:(id)sender {
[self.delegate onSave:self.NameText.text];
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import "GreenViewController.h"
#interface ViewController : UIViewController <GreenViewDelegate>
#property (weak, nonatomic) IBOutlet UISlider *slider;
#property (weak, nonatomic) IBOutlet UILabel *sliderTextValue;
#property (weak, nonatomic) IBOutlet UIButton *EnterName;
#property (weak, nonatomic) IBOutlet UILabel *NameValue;
#property (weak, nonatomic) IBOutlet UIButton *LikeIOS;
#property (weak, nonatomic) IBOutlet UISwitch *CheckboxIOS;
#end
ViewController.m
#import "ViewController.h"
#import "RedViewController.h"
#import "GreenViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.sliderTextValue.text = [NSString stringWithFormat:#"Slider value = %d",(int)self.slider.value];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"yellowToGreen"]){
GreenViewController* gVC = segue.destinationViewController;
gVC.sliderValuePassed = [NSString stringWithFormat:#"Slider value = %d",(int)self.slider.value];
gVC.delegate = self;
}else if([segue.identifier isEqualToString:#"yellowToRed"]){
RedViewController* rVC = segue.destinationViewController;
rVC.sliderValuePassed = [NSString stringWithFormat:#"Slider value = %d",(int)self.slider.value];
rVC.CheckboxIOS = self.CheckboxIOS;
}
}
- (IBAction)sliderChangeValue:(id)sender {
int sliderVal = self.slider.value;
self.sliderTextValue.text = [NSString stringWithFormat:#"Slider value = %d",sliderVal];
}
-(void)onSave:(NSString*)nameValue{
NSLog(#"onSave in father controller");
self.NameValue.text = [NSString stringWithFormat:#"Name = %#",nameValue];
}
#end
I assume you didnt set the delegate .
Set it like this:
GreenViewController *myVc = [[GreenViewController alloc] init];
myVc.delegate = self;
Put this in your view controller view did load method!!!
try with GreenViewController.h:
#property (nonatomic, assign) id<GreenViewDelegate> delegate;
and GreenViewController.m:
- (IBAction)save:(id)sender {
[_delegate onSave:self.NameText.text];
[self.navigationController popViewControllerAnimated:YES];
}
ViewController.* seems to be OK

accessing IBOutlet properties from another class

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

Show image in ViewController when button is clicked in SecondViewController

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]];
}

Cannot find protocol declaration for changeMapTyp iOS

I currently get the error "cannot find protocol declaration for changeMapTyp" and I have no idea why. I imported all the required files, but it hasn't solved my problem.
As mentioned in my comments, the error message disappears when I delete the #import MapsViewController.h in the MapsBackgroundViewController.h, but I can´t delete the line, because I need to write this lines
MapsViewController *myMapsViewController = [[MapsViewController alloc] init];
[self setDelegate:myMapsViewController];
to set my delegate. Am I right?
Here is my code:
MapsBackgroundViewcontroller.h
#import "MapsViewController.h"
#protocol ChangeMapTyp <NSObject>
#required
- (void)segmentedControllChangedMapType:(MKMapType) type ;
#end
#interface MapBackgroundViewController : UIViewController{
IBOutlet UISegmentedControl *segmentedControl;
MKMapType mapType;
id < ChangeMapTyp> delegate;
}
#property (nonatomic) IBOutlet UISegmentedControl *segmentedControl;
#property(nonatomic) MKMapType mapType;
#property(nonatomic)id delegate;
- (IBAction)segmentedControllChanged:(id)sender;
MapsBackgroundViewController.m
#import "MapBackgroundViewController.h"
#interface MapBackgroundViewController ()
#end
#implementation MapBackgroundViewController
#synthesize segmentedControl, mapType, delegate;
- (void)viewDidLoad
{
[super viewDidLoad];
// that´s why I can´t delete the import ???
MapsViewController *myMapsViewController = [[MapsViewController alloc] init];
[self setDelegate:myMapsViewController];
}
- (IBAction)segmentedControllChanged:(id)sender {
if (segmentedControl.selectedSegmentIndex == 0) {
mapType = MKMapTypeStandard;
}else if (segmentedControl.selectedSegmentIndex == 1) {
mapType = MKMapTypeSatellite;
} else if (segmentedControl.selectedSegmentIndex == 2) {
// [self.delegate setMapType:MKMapTypeHybrid];
mapType = MKMapTypeHybrid;
}
//Is anyone listening
if([delegate respondsToSelector:#selector(segmentedControllChangedMapType:)])
{
//send the delegate function with the amount entered by the user
[delegate segmentedControllChangedMapType:mapType];
}
[self dismissModalViewControllerAnimated:YES];
}
MapsViewController.h
#import "MapBackgroundViewController.h"
#class MapBackgroundViewController;
#interface MapsViewController : UIViewController<MKMapViewDelegate,
UISearchBarDelegate, ChangeMapTyp >{ //here i get the error message
#private
IBOutlet MKMapView *map;
}
#property (nonatomic, retain) IBOutlet MKMapView *map;
#end
MapsViewController.m
#import "MapBackgroundViewController.h"
#interface MapsViewController ()
#end
#implementation MapsViewController
#synthesize map;
- (void)segmentedControllChangedMapType: (MKMapType) type{
map.mapType = type;
}
In MapsViewController.h, you need to have -
#import "MapBackgroundViewController.h" //Keep This line
#class MapBackgroundViewController; // Remove this line
#interface MapsViewController : UIViewController<MKMapViewDelegate,UISearchBarDelegate, ChangeMapTyp >{
Hope this will fix your issue.

Resources