Method in protocol not implemented in iOS - ios5

I do not understand because the error appears "Method in protocol not implemented"
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,strong) UIImageView *myImage;
#property(nonatomic,weak) 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

You call the delegate method inside the SecondViewController but you didn't insert it. If you get a warning like ... not implemented it just says, you forgot to include a method.
You could insert the method like this
-(UIImage *)transferImage{
//do something here if delegate has been called
}
or you just add a parameter above the method inside your delegate block:
#optional
if you don't specify it, all methods will be set to #required initial.

it is as simple as you have declared a method in your protocol implementation but have not implemented the same. In your Second VC you have not implemented the method "tranfer image" . So the compiler is generating a warning

Related

Passing control between two views in iphone [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Passing model objects from one view controller to another in a navigation stack
I am trying to pass the control between two views in iphone. I have tried this and it works fine when you pass from the first view controller to the second but when i click the second, it blanks out. Why is that so? Any help is appreciated.. Thanks...
Viewcontroller.h
#import <UIKit/UIKit.h>
#import "ViewController2nd.h"
#interface ViewController : UIViewController <SecondViewControllerDelegate>
{
IBOutlet UILabel *lbl;
}
-(IBAction)passdata:(id)sender;
#end
Viewcontroller.m
#import "ViewController.h"
#import "ViewController2nd.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) changeLabel:(NSString*)str{
lbl.text = #"Hello";
}
-(IBAction)passdata:(id)sender{
ViewController2nd *second = [[ViewController2nd alloc] initWithNibName:nil bundle:nil];
[self presentViewController:second animated:YES completion:^{ }];
}
#end
ViewController2nd.h
#import <UIKit/UIKit.h>
#protocol SecondViewControllerDelegate <NSObject>
#optional
-(void) changeLabel:(NSString*)str;
#end
#interface ViewController2nd : UIViewController{
IBOutlet UIButton *bttn;
}
-(IBAction)bttnclicked;
-(IBAction)back:(id)sender;
#end
ViewController2nd.m
#import "ViewController2nd.h"
#import "ViewController.h"
#interface ViewController2nd ()
#end
#implementation ViewController2nd
- (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 from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)bttnclicked{
}
-(IBAction)back:(id)sender{
ViewController *first = [[ViewController alloc] initWithNibName:nil bundle:nil];
[self presentmodalViewController:first animated:YES completion:^{ }];
}
#end
Am i missing something?
If your requirement is to go to the first view change the back: method like:
-(IBAction)back:(id)sender
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
Don't display the parentView as the childView of the chilkdViewController. It also makes memory issues and logical issues.
So if you want to go from the childView to parentView, dismiss the childView and never create the parent object and present it there.

Trying to abstract code out into a library, but my code isn't working and I do not know why

I want to make a library that I can reuse over and over to speed up my development, add to new projects if I wish. Basically I want to build an abstraction layer. Can someone show me how I would do that and why this this portion of code:
if ([self.delegate respondsToSelector:#selector(enableCamera)]) {
BOOL enabled;
enabled = [self.delegate enableCamera];
if (enabled == YES) {
[self enableCameraMethod];
}
does not get called?
HERES MY CODE BELOW:
library.h:
#protocol usesCamera <NSObject>
#optional
-(BOOL)enableCamera;
#end
#interface Library : NSObject
#property (nonatomic, weak) id <usesCamera> delegate;
-(void)enableCameraMethod;
#end
library.m
#import "Library.h"
#implementation Library
- (id) init
{
if (self = [super init]) {
if ([self.delegate respondsToSelector:#selector(enableCamera)]) {
BOOL enabled;
enabled = [self.delegate enableCamera];
if (enabled == YES) {
[self enableCameraMethod];
}
}
return (self);
}
}
-(void)enableCameraMethod {
NSLog(#"Implement my camera method here");
}
#end
UIViewController.h
#import <UIKit/UIKit.h>
#import "Library.h"
#interface ViewController : UIViewController <usesCamera>
#end
UIViewController.m
#import "ViewController.h"
#import "Library.h"
#interface ViewController ()
#property (nonatomic, strong) UIViewController *myVC;
#end
#implementation ViewController
-(BOOL)enableCamera {
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
Library *myLibrary = [[Library alloc] init];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
did you set your delegate for myLibrary instance in your ViewController class.
you have to do something like this:
Library *myLibrary = [[Library alloc] init];
myLibrary.delegate = self;
As init is called before setting the delegate, so it might not worked, instead of defining the logic in init function create another method and call this method after setting the delegate.

Passing data between Tabs using delegate not working

I want to pass data from second tab to first tab using delegates.But delegate method not triggered.here is the code (I m using storyboard)
in SecondViewController.h
#import <UIKit/UIKit.h>
#class SecondViewController;
#protocol SecondViewControllerDelegate <NSObject>
-(void) sliderValueDidChanged: (SecondViewController *)controller data:(float) sliderValue;
#end
#interface SecondViewController : UIViewController{
__weak id<SecondViewControllerDelegate> delegate;
}
- (IBAction)sliderPressed:(id)sender;
#property (weak, nonatomic) IBOutlet UISlider *mySlider;
#property(weak,nonatomic) id<SecondViewControllerDelegate> delegate;
#end
in SecondViewController.m
#import "SecondViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
#synthesize mySlider;
#synthesize delegate;
- (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.
}
- (void)viewDidUnload
{
[self setMySlider:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)sliderPressed:(id)sender {
float value = mySlider.value;
[self.delegate sliderValueDidChanged:self data:value];
NSLog(#"%#",self.delegate);
}
#end
FirstViewController.h
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
#interface FirstViewController : UIViewController<SecondViewControllerDelegate>
#property (weak, nonatomic) IBOutlet UILabel *myLabel;
#end
in FirstViewController.h
#import "FirstViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
#synthesize myLabel;
- (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.
SecondViewController *svc = [[SecondViewController alloc]init];
svc.delegate = self;
}
- (void)viewDidUnload
{
[self setMyLabel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void) sliderValueDidChanged: (SecondViewController *)controller data:(float) sliderValue{
myLabel.text = [[NSString alloc]initWithFormat:#"%f",sliderValue];
NSLog(#"delegate called");
}
#end
I am trying to set a slider in second tab and send the sider value to first tab which has a label. self.delegate printing null and delegate method never called.
Simply enough, as you already have the delegation code, only change the line where you create a SecondViewController object. Rather than creating a new one, you should have a reference to the one that the tab bar will show.
In the viewDidLoad of FirstViewController,
Change
SecondViewController *svc = [[SecondViewController alloc]init];
to
//assuming you have 2 view controllers in the tab bar controller, first being FirstViewController
SecondViewController *svc = [self.tabBarController.viewControllers objectAtIndex:1];
and that should do it
How do you actually get to the second tab? You're creating an instance of it in viewDidLoad, but if you switch tabs, that instance won't be used to show on screen. The system will create another instance which it'll use. You can access this by accessing the tab bar controller's viewcontrollers property and checking for SecondViewController.
EDIT: You could also question your design. I don't know much about your app, but chances are your first tab shouldn't really be the delegate for your second tab. If you want to pass along data, consider using NSNotificationCenter or persistent storage.

my custom viewcontroller delegate not working

I am trying to send a simple string from One viewcontroller to another viewcontroller using delegate but my delegate is not working. I am using storyboard and dont want to use segue to pass data. Here is my simple code
ViewControllerA.h
#import <UIKit/UIKit.h>
#class ViewControllerA;
#protocol viewControllerADelegate <NSObject>
-(void) addItem: (ViewControllerA *)controller data:(NSString *)item;
#end
#interface ViewControllerA : UIViewController{
__weak id <viewControllerADelegate> delegate;
}
- (IBAction)buttonPressed:(id)sender;
#property (weak) id<viewControllerADelegate> delegate;
#end
ViewControllerA.m
#import "ViewControllerA.h"
#interface ViewControllerA ()
#end
#implementation ViewControllerA
#synthesize delegate;
- (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.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)buttonPressed:(id)sender {
[self.delegate addItem:self data:#"this is data"];
}
#end
ViewControllerB.h
#import <UIKit/UIKit.h>
#import "ViewControllerA.h"
#interface ViewControllerB : UIViewController<viewControllerADelegate>
#end
ViewControllerB.m
#import "ViewControllerB.h"
#import "ViewControllerA.h"
#interface ViewControllerB ()
#end
#implementation ViewControllerB
- (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.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
ViewControllerA *vca = [storyboard instantiateViewControllerWithIdentifier:#"A"];
[vca setDelegate:self];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void) addItem: (ViewControllerA *)controller data:(NSString *)item{
NSLog(#"delegate function called");
}
#end
Am I missing something silly ? The delegate method -(void) addItem: (ViewControllerA *)controller data:(NSString *)item is not triggered.
The flaw in your design is to try sending data with delegates to an object you create yourself. You certainly don't need delegates for that as you can do it with simple properties. myViewControllerBObject.dataProperty = #"some data"]; is simply enough. What delegation is usually used for is send data back to the controller that created the current object or to any other controller somewhere in your app.
For example, move the delegate code in ViewControllerA to ViewControllerB and add a UIButton and an IBAction to ViewControllerB that will send the data back to ViewControllerA. Here it goes:
ViewControllerB.h
#import <UIKit/UIKit.h>
#class ViewControllerB;
#protocol viewControllerBDelegate <NSObject>
-(void) sendDataBack: (ViewControllerB *)controller data:(NSString *)item;
#end
#interface ViewControllerB : UIViewController
{
__unsafe_unretained id <viewControllerBDelegate> delegate;
}
#property (nonatomic, assign) id<viewControllerBDelegate> delegate;
- (IBAction)buttonTouch:(id)sender;
#end
ViewControllerB.m
#import "ViewControllerB.h"
#interface ViewControllerB ()
#end
#implementation ViewControllerB
#synthesize delegate;
....
- (IBAction)buttonTouch:(id)sender {
[self.delegate sendDataBack:self data:#"my data"];
}
#end
And in ViewControllerA, you first need to get a hold of that pushed ViewControllerB object via segues. Then implement the delegate method.
ViewControllerA.h
#import <UIKit/UIKit.h>
#import "ViewControllerB.h"
#interface ViewControllerA : UIViewController <viewControllerBDelegate>
- (IBAction)buttonPressed:(id)sender;
#end
ViewControllerA.m
#import "ViewControllerA.h"
#interface ViewControllerA ()
#end
#implementation ViewControllerA
......
- (IBAction)buttonPressed:(id)sender {
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(#"id %#", [segue destinationViewController]);
ViewControllerB *vcb = (ViewControllerB *)[segue destinationViewController];
vcb.delegate = self; //key point
}
- (void)sendDataBack:(ViewControllerB *)controller data:(NSString *)item
{
NSLog(#"Here is the data %# and the controller it comes from %#", item, controller);
}
#end
Hope this will help you get a better understanding of communication with delegates.
Make the delegate variable (strong, nonatomic) instead of (__weak).

Multi-View Application Problem

I want to make multi view application. My goal is when program launch to load first view controller and then when press the button to load new tab bar controller and dismiss first controller. I try myself and I do firt step, but when tab bar controller is loaded it's appear only small tab without any tabs and old controller doesn't disappear.
This is what I've done:
SwitchAppelegate
-- Header file --
#import <UIKit/UIKit.h>
#class SwitchClass;
#interface SwitchAppDelegate : NSObject <UIApplicationDelegate> {
SwitchClass *switchClass;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet SwitchClass *switchClass;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
#end
-- Implementation file --
#import "SwitchAppDelegate.h"
#implementation SwitchAppDelegate
#synthesize window=_window;
#synthesize managedObjectContext=__managedObjectContext;
#synthesize managedObjectModel=__managedObjectModel;
#synthesize persistentStoreCoordinator=__persistentStoreCoordinator;
#synthesize switchClass;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self.window addSubview:[switchClass view]];
[self.window makeKeyAndVisible];
return YES;
}
SwitchClass
-- Header file --
#import <UIKit/UIKit.h>
#class BlueClass;
#interface SwitchClass : UIViewController {
BlueClass *blueClass;
}
#property (nonatomic, retain) IBOutlet BlueClass *blueClass;
#end
-- Implementation file --
#import "SwitchClass.h"
#import "BlueClass.h"
#implementation SwitchClass
#synthesize blueClass;
-(void) viewDidLoad {
BlueClass *blue = [[BlueClass alloc] initWithNibName:#"BlueClass" bundle:nil];
self.blueClass = blue;
[self.view insertSubview:blue.view atIndex:0];
[blue release];
[super viewDidLoad];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[blueClass release];
[super dealloc];
}
BlueClass
-- Header file --
#class RedClass;
#interface BlueClass : UIViewController {
RedClass *redClass;
}
#property (nonatomic, retain) IBOutlet RedClass *redClass;
-(IBAction) switch: (id) sender;
#end
-- Implementation file --
#import "BlueClass.h"
#import "RedClass.h"
#implementation BlueClass
#synthesize redClass;
-(IBAction) switch: (id) sender {
RedClass *blue = [[RedClass alloc] initWithNibName:#"RedClass" bundle:nil];
self.redClass = blue;
// self.window.rootViewController = self.tabBarController;
[self.view addSubview:blue.view];
[blue release];
[super viewDidLoad];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[redClass release];
[super dealloc];
}
RedClass
-- Header file --
#import <UIKit/UIKit.h>
#interface RedClass : UITabBarController {
}
#end
-- Implementation file --
#import "RedClass.h"
#implementation RedClass
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
First thing is that in the appDelegate you are adding the view of SwitchClass. But SwitchClass is a UIViewController class and not UIView. so instead of that you should add your SwitchClass as a rootController like this:
self.window.rootViewController = self.switchClass;
If I were you I would just use a tab bar template provided by XCode. It will do it for you automatically.