Delegate not informing my View - objective-c

My cpViewController has a button that modal segues to cpScanViewController. I want to have cpScanViewController inform ViewController with a string when it has completed a successful scan. After reading numerous articles online, I believe delegation is the best way to do this?
Is the delegate being set correctly?
How does the delegate inform the cpViewController?
cpScanViewController.h
#import <UIKit/UIKit.h>
#protocol ScanDelegate <NSObject>
-(void)receivedUPC:(NSString *)Scan;
#end
#interface cpScanViewController : UIViewController
#property (nonatomic, weak) id <ScanDelegate> delegate;
#property (nonatomic, retain) NSString *UPC;
-(void)checkUPC;
#end
cpScanViewController.m
#interface cpScanViewController ()
{
NSString *UPC;
}
#end
#implementation cpScanViewController
#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)doSomeScanStuff
{
UPC = #"a string"
// even tried
[delegate receivedUPC:UPC];
}
-(void)checkUPC
{
}
#end
cpViewController.h
#import <UIKit/UIKit.h>
#import "cpScanViewController.h"
#interface cpViewController : UIViewController <ScanDelegate>
#end
cpViewController.m
#interface cpViewController ()
{
cpScanViewController *cp;
}
#end
#implementation cpViewController
- (void)viewDidLoad
{
// set delegate
cp = [[cpScanViewController alloc] init];
[cp setDelegate:self];
}
-(void)receivedUPC:(NSString *)Scan{
// Nothing happening
NSLog(#"%#", Scan);
NSLog(#"The %#", cp.UPC);
}
#end

Because you are not calling receivedUPC method from your cpScanViewController class. You need to add [_delegate receivedUPC:self], some where in order to invoke this delegat method in cpViewController class.

I was able to get it working by using the method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
cpScanViewController *vd = (cpScanViewController *)[segue destinationViewController];
vd.delegate = self;
}
Because I made the connection in storyboards I didn't realize it needed to have the method.

Related

Pop up not Dismissing when i click button in logout

I am using pop over button to Signout a page back to Root view controller. i successfully came back to root view controller but pop up not dismissing. It stays on the Root view controller Here is my code
POPOVERVIEW CONTROLLER.H
#protocol MJSecondPopupDelegate;
#import <UIKit/UIKit.h>
#import "LoginViewController.h"
#import "TimeTableViewController.h"
#class TimeTableViewController;
#interface PopOverViewController : UIViewController <UIPopoverControllerDelegate>
{
// UIPopoverController *popoverController;
}
#property (assign, nonatomic) id <MJSecondPopupDelegate>delegate;//ede ga
#property(strong,nonatomic) UIPopoverController *popoverController;
#property(nonatomic,strong)TimeTableViewController *TimeObj;
-(IBAction)logOut:(UIButton *)sender;
#end
#protocol MJSecondPopupDelegate<NSObject>
#optional
- (void)cancelButtonClicked:(PopOverViewController*)secondDetailViewController;
#end
POPOVERVIEW CONTROLLER.M
#import "PopOverViewController.h"
#interface PopOverViewController ()
#end
#implementation PopOverViewController
#synthesize delegate,TimeObj,popoverController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(void)SignOut{
NSLog(#"protocol");
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"delegate value is %#",self.delegate);
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)logOut:(UIButton *)sender{
if (self.delegate && [self.delegate respondsToSelector:#selector(cancelButtonClicked:)])
{
[self.delegate cancelButtonClicked:self];
[self.popoverController dismissPopoverAnimated:YES];
}
}
MAINVIEW CONTROLLER.H
#import <UIKit/UIKit.h>
#import "NotesandReminders.h"
#import "Cell.h"
#import "SWRevealViewController.h"
#import "AvailableSessionViewController.h"
#import "ServiceConnector.h"
#import "AppDelegate.h"
#import "PopOverViewController.h"
#import "UIViewController+MJPopupViewController.h"
#class NotesandReminders;
#class PopOverViewController;
#protocol Popupprotocol <NSObject>
#optional
-(void)SignOut;
#end
#interface TimeTableViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate,MJSecondPopupDelegate>{
}
MAINVIEWCONTROLLER.M
#import "AvailableSessionViewController.h"
#interface TimeTableViewController ()
#end
#implementation TimeTableViewController
#synthesize pc,myCounterLabel,cancelSessionButtonOutlet,userButtonOutlet,saveButtonOutlet,timerr,collectionData,cancelSession,jsonData,secondviewcontroller,popoverController;
#synthesize startWeek,endWeek,startDateLabel,endDateLabel,todaysDate,startDate,endDate,dateForMatching,startDateToCall,endDateToCall;
#synthesize blueBarOutlet;
BOOL isLongPressed ;
int hours, minutes, seconds;
int secondsLeft;
int valueForBlueBar;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
// This method runs first when the screen is displayed
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"POPSegue"]) {
PopOverViewController* userViewController = [segue destinationViewController];
userViewController.delegate=self;
}
}
I am creating Pop over just by cntrl drag to the view controller and selecting PopOver modally
After the following line:
[self.popoverController dismissPopoverAnimated:YES];
add
[self.popoverController.contentViewController.navigationController
popToRootViewControllerAnimated:YES];
This is how i managed to get rid of it
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"POPSegue"]) {
// settings popover is the global variable for --> UIPopoverController *settingsPopOver;
settingsPopOver = [(UIStoryboardPopoverSegue *)segue popoverController];
_secondviewcontroller = (PopOverViewController *)settingsPopOver.contentViewController;
_secondviewcontroller.delegate = self;
}
}

Mac OS X using WebView object

I can't see the webview when I launch tha app..
In the delegate that's my code
#import <Cocoa/Cocoa.h>
#include "NewsViewController.h"
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (nonatomic,strong) IBOutlet NSSplitView *splitView;
#property (nonatomic,strong) IBOutlet NewsViewController *newsViewController;
#end
#import "AppDelegate.h"
#implementation AppDelegate
- (void)dealloc
{
[super dealloc];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
// 1. Create the master View Controller
self.newsViewController = [[NewsViewController alloc] initWithNibName:#"NewsViewController" bundle:nil];
// 2. Add the view controller to the Window's content view
[self.splitView addSubview:self.newsViewController.view];
}
#end
That's my viewcontroller
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface NewsViewController : NSViewController{
IBOutlet WebView *web;
}
#property (assign) IBOutlet WebView *web;
#end
#import "NewsViewController.h"
#interface NewsViewController ()
#end
#implementation NewsViewController
#synthesize web;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
}
return self;
}
-(void)loadView{
NSString *urlAddress = #"http://www.google.com/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[[web mainFrame] loadRequest:requestObj];
}
#end
if I out one label on my view in the controller everything is fine but if I put a webview I see only a grey window.
Why this?
thanks
I do believe you need to call [super loadView] in your loadView implementation.

Objective C Empty Array in Different Class

Question with trying to simply clear a mutablearray in another class in objective C
the code all runs fine, just the data doesnt get wiped....
SecondViewController.h
#interface SecondViewController : UIViewController <UITableViewDelegate,UITextFieldDelegate,ABPeoplePickerNavigationControllerDelegate>
{
AppDelegate *appDelegate;
}
#property (nonatomic, retain) NSMutableArray *contactsArray;
Thats the declaration of the mutablearray...
then from the class that i am trying to delete it.
listViewController.h
#import "SecondViewController.h"
#interface ListViewController : UIViewController <UITableViewDelegate,ABPeoplePickerNavigationControllerDelegate,UITabBarControllerDelegate>{
AppDelegate *appDelegate;
SecondViewController *secondViewControl;
}
#property (nonatomic, retain) IBOutlet UITableView *table;
then in the main file i use the code
[secondViewControl.contactsArray removeAllObjects];
but then when i check the array it has not been emptied...
EDIT:
#implementation ListViewController
#synthesize table;
/*- (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.
appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
secondViewControl = [[SecondViewController alloc]init];

No information is passed to the DetailView Objective-c

my problem - I want to pass information from the TableView in DetailView, but somewhere in the self.detailViewController.detailItem no value is passed.
that is:
self.detailViewController.detailItem =#"123";
NSString *sss = self.detailViewController.detailItem;
NSLog(#"%#", sss);
In NSLog output (null)
Here is my source code:
DetailViewController.h
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController <UISplitViewControllerDelegate>
#property id detailItem;
#property id TitleOfDetail;
#property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
#end
DetailViewController.m
#import "DetailViewController.h"
#interface DetailViewController ()
#property (strong, nonatomic) UIPopoverController *masterPopoverController;
- (void)configureView;
#end
#implementation DetailViewController
#pragma mark - Managing the detail item
-(void)setTitleOfDetail:(id)newTitleOfDetail {
NSLog(#"123");
if (_TitleOfDetail != newTitleOfDetail) {
_TitleOfDetail = newTitleOfDetail;
// Update the view.
[self configureView];
}
}
-(void)setDetailItem:(id)newDetailItem
{
NSLog(#"123");
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
if (self.masterPopoverController != nil) {
self.title = self.TitleOfDetail;
[self.masterPopoverController dismissPopoverAnimated:YES];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
NSLog(#"1%#", self.TitleOfDetail);
NSLog(#"2%#", [self.detailItem description]);
if (self.TitleOfDetail) {
self.title = self.TitleOfDetail;
}
if (self.detailItem) {
self.detailDescriptionLabel.text = [self.detailItem description];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
…
myclass.h
#class DetailViewController;
#interface myclass : UITableViewController
#property DetailViewController *detailViewController;
#end
myclass.m
#import "choiseAvtoController.h"
#import "DetailViewController.h"
#interface myclass ()
#end
#implementation myclass
#synthesize detailViewController;
…
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.detailViewController.detailItem =#"123";
NSString *sss = self.detailViewController.detailItem;
NSLog(#"%#", sss);
}
In what may be the problem and how to fix it?
P.S. Sorry for my english, I'm just learning))
Since you are using a storyboard, you should be opening the detail controller on a segue.
Look up the segue identifier (say, it's OpenDetail) in the story board, then add the following code to your initial controller:
-(void)prepareForSegue:(UIStoryboardPopoverSegue*)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"OpenDetail"]) {
DetailViewController *dest = segue.destinationViewController;
dest.detailItem =#"123";
}
}

My delegate action is not called in Cocoa

I have a class which has it's delegate:
#protocol SNIRCControllerDelegate
- (void) serverTalked:(id)data;
#end
#interface SNIRCController : NSObject <NSStreamDelegate> {
id<SNIRCControllerDelegate> delegate;
}
- (void) setDelegate:(id<SNIRCControllerDelegate>)_delegate;
- (void) test;
#end
The implementation:
#implementation SNIRCController
- (void) setDelegate:(id<SNIRCControllerDelegate>)_delegate {
_delegate = delegate;
}
- (void) test {
[delegate serverTalked:#"test"];
}
But for some reason [delegate serverTalked:#"test"]; doesn't calls the delegate :/
This is how I do it on the AppDelegate:
#interface AppDelegate : NSObject <NSApplicationDelegate, NSStreamDelegate, SNIRCControllerDelegate> {
IBOutlet NSTextView *logField;
SNIRCController *ircController;
}
#property (assign) IBOutlet NSWindow *window;
-(void)writeToLog:(NSString*)data;
#end
#implementation AppDelegate
#synthesize window = _window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
ircController = [[SNIRCController alloc] init];
[ircController setDelegate:self];
[ircController test];
}
- (void) serverTalked:(id)data {
NSLog(#"got called :D");
}
-(void)writeToLog:(NSString*)data {
NSAttributedString *stringToAppend = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#\n", data]];
[[logField textStorage] appendAttributedString:stringToAppend];
}
But serverTalked: doesn't gets called :( what I'm doing wrong?
In your implementation of setDelegate:, this:
_delegate = delegate;
should be:
delegate = _delegate;
You got confused and swapped ivar and parameter. The confusion might have been caused by the fact that the underscore prefix is more commonly used for ivars. In your case, it is the parameter that is prefixed with an underscore.