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";
}
}
Related
Problem in getting Navigation Bar using JSQMessagesViewController, I have tried getting Navigation Bar in my project, but failed to get it. Please let me know what the methods need to be added to get it.
#import "JSQMessagesCollectionViewFlowLayout.h"
#import "JSQMessages.h"
#import "JSQPhotoMediaItem.h"
#import "JSQLocationMediaItem.h"
#import "JSQVideoMediaItem.h"
#import "JSQMessagesMediaViewBubbleImageMasker.h"
#import "JSQMessagesAvatarImage.h"
#import "JSQMessagesAvatarImageFactory.h"
#import "JSQMessagesBubbleImage.h"
#import "JSQMessagesBubbleImageFactory.h"
#import "UIImage+JSQMessages.h"
#import "Chat.h"
#interface ChatpageViewController : JSQMessagesViewController
{
NSMutableArray * fularray;
}
#property(nonatomic,strong)NSDictionary * receivedict;
#property (strong, nonatomic) IBOutlet UILabel *name;
#property (strong, nonatomic) IBOutlet UILabel *mobile;
#property (strong, nonatomic) JSQMessagesBubbleImage *outgoingBubbleImageData;
#property (strong, nonatomic) JSQMessagesBubbleImage *incomingBubbleImageData;
#end
#import "ChatpageViewController.h"
#interface ChatpageViewController ()
#end
#implementation ChatpageViewController
#synthesize receivedict,name,mobile;
//-(void)viewWillAppear:(BOOL)animated
//{
//
// // self.collectionView.collectionViewLayout.springinessEnabled = YES;
//}
(void)viewDidLoad
{
[super viewDidLoad];
fularray = [[NSMutableArray alloc] init];
// Do any additional setup after loading the view.
// [self.navigationController setNavigationBarHidden:NO animated:YES];
NSLog(#"%#",receivedict);
name.text = [receivedict objectForKey:#"Name"];
mobile.text =[receivedict objectForKey:#"Mobile"];
JSQMessagesBubbleImageFactory *bubbleFactory = [[JSQMessagesBubbleImageFactory alloc] init];
self.outgoingBubbleImageData = [bubbleFactory outgoingMessagesBubbleImageWithColor:[UIColor jsq_messageBubbleBlueColor]];
self.incomingBubbleImageData = [bubbleFactory incomingMessagesBubbleImageWithColor:[UIColor jsq_messageBubbleLightGrayColor]];
}
(id)collectionView:(JSQMessagesCollectionView *)collectionView messageBubbleImageDataForItemAtIndexPath:(NSIndexPath *)indexPath
{
JSQMessage *message = [fularray objectAtIndex:indexPath.item];
if ([message.senderId isEqualToString:self.senderId]) {
return self.outgoingBubbleImageData;
}
return self.incomingBubbleImageData;
}
(id)collectionView:(JSQMessagesCollectionView *)collectionView avatarImageDataForItemAtIndexPath:(NSIndexPath *)indexPath {
return [JSQMessagesAvatarImageFactory avatarImageWithUserInitials:#"JL" backgroundColor:[UIColor blueColor] textColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:12.0] diameter:30.0];
}
(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [fularray count];
}
(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
(id)collectionView:(JSQMessagesCollectionView *)collectionView messageDataForItemAtIndexPath:(NSIndexPath *)indexPath {
return fularray[indexPath.row];
}
(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
(NSString *)senderId
{
return [receivedict objectForKey:#"Mobile"];
}
(NSString *)senderDisplayName
{
return [receivedict objectForKey:#"Name"];
}
(NSDate *)date
{
return 18/03/2016;
}
(void)didPressSendButton:(UIButton *)button withMessageText:(NSString *)text senderId:(NSString *)senderId senderDisplayName:(NSString *)senderDisplayName date:(NSDate *)date
{
[JSQSystemSoundPlayer jsq_playMessageSentSound];
JSQMessage *message = [[JSQMessage alloc] initWithSenderId:senderId
senderDisplayName:senderDisplayName
date:date
text:text];
[fularray addObject:message];
[self.collectionView reloadData];
[self finishSendingMessageAnimated:YES];
NSLog(#"%#",message);
}
Myproject is saved in the link :https://www.dropbox.com/s/ozih8ko9836fmb2/chatapp.zip?dl=0
Goto MainStoryboard->select your Controller-> in menu choose Editor-> Embed in -> Navigation Controller
Change in JSQMessagesViewController.m file in -(void)jsq_updateCollectionViewInsets
[self jsq_setCollectionViewInsetsTopValue:self.topContentAdditionalInset + 64 bottomValue:CGRectGetMaxY(self.collectionView.frame) - CGRectGetMinY(self.inputToolbar.frame)];
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.
I am writing an app that is transferring data from one class to another. For some reason the text is not being displayed on the other view when it is supposed to.
My code:
.h Master class:
#import <UIKit/UIKit.h>
#class SSWSettings;
#interface SSWSelectProgram : UITableViewController
#property (strong, nonatomic) SSWSettings *detailViewController;
#property (weak, nonatomic) IBOutlet UILabel *p1;
#end
.m master class:
#import "SSWSelectProgram.h"
#import "SSWSettings.h"
#interface SSWSelectProgram ()
#end
#implementation SSWSelectProgram
#synthesize p1;
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cellChosen = [self.tableView cellForRowAtIndexPath:indexPath];
if (cellChosen == static1) {
self.detailViewController.detailItem = p1.text;
[self dismissViewControllerAnimated:YES completion:NULL];
}
.h detail class
#import <UIKit/UIKit.h>
#interface SSWSettings : UITableViewController <UITableViewDelegate, UITableViewDataSource>
#property (strong, nonatomic) id detailItem;
#property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
#end
.m detail class
#import "SSWSettings.h"
#import "SSWSelectProgram.h"
#interface SSWSettings ()
-(void)configureView;
#end
#implementation SSWSettings
#synthesize detailDescriptionLabel;
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = [self.detailItem description];
}
}
- (void)viewDidAppear:(BOOL)animated
{
[self configureView];
}
Your object "detailItem" is of type id.
Thus, it does not inherit from NSObject and the
[self.detailItem description];
call Will not return anything.
Try
self.detailDescriptionLabel.text = (NSString*) self.detailItem;
A better solution, of course, is to not use the type 'id' and use NSString
I have a simple utility app, with a MainViewController.m & h and a FlipsideViewController.m & h. Within my storyboard I have a button on MainViewController. I want to be able to click the button and run a method in FlipsideViewController.m is this possible? this is my first app and I am a total novice. all comments / suggestion welcome.
enter code here
i have this in my FlipsideViewController.m this is what i want to call when i click the button.
- (void)SaveFPQData
{
NSLog(#"Data Saved");
}
and this is what i have in MainViewController.m
- (IBAction)saveButton:(id)sender
{
}
This is the code I have so far;
MainViewController.h
#import "FlipsideViewController.h"
#import "sqlite3.h"
#import "FPQCheck.h"
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate>
#property (weak, nonatomic) IBOutlet UITextField *nameField;
#property (weak, nonatomic) IBOutlet UITextField *checkField;
#property (weak, nonatomic) IBOutlet UITextField *commentsField;
#property (weak, nonatomic) FlipsideViewController *flipsidecontroller;
- (IBAction)saveButton:(id)sender;
- (IBAction)showHistoryButton:(id)sender;
#end
MainViewController.m
#import "MainViewController.h"
#import "FlipsideViewController.h"
#import "sqlite3.h"
#interface MainViewController ()
#end
#implementation MainViewController
- (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.
}
#pragma mark - Flipside View
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showAlternate"]) {
[[segue destinationViewController] setDelegate:self];
}
}
- (IBAction)saveButton:(id)sender
{
[self.flipsidecontroller SaveFPQData];
//[[NSNotificationCenter defaultCenter] postNotificationName:#"SaveFPQData" object:nil];
}
- (IBAction)showHistoryButton:(id)sender
{
}
#end
FlipSideViewController.h
#import <UIKit/UIKit.h>
#import "FPQCheck.h"
#class FlipsideViewController;
#protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
#end
#interface FlipsideViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *myTableView;
#property (weak, nonatomic) id <FlipsideViewControllerDelegate> delegate;
-(void)SaveFPQData;
- (IBAction)done:(id)sender;
- (IBAction)deleteEntry:(id)sender;
#end
FlipSideViewController.m
#import "FlipsideViewController.h"
#import "MainViewController.h"
#interface FlipsideViewController ()
{
NSMutableArray *arrayOfCheck;
sqlite3 *fpqDB;
NSString *dbPathString;
}
#end
#implementation FlipsideViewController
- (void)viewDidLoad
{
/*
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(SaveFPQData)
name:#"SaveFPQData"
object:nil];
*/
[super viewDidLoad];
arrayOfCheck = [[NSMutableArray alloc]init];
[self creatOrOpenDB];
[[self myTableView]setDelegate:self];
[[self myTableView]setDataSource:self];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)SaveFPQData
{
NSLog(#"Data Saved");
}
-(void)creatOrOpenDB
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *docPath = [path objectAtIndex:0];
dbPathString = [docPath stringByAppendingPathComponent:#"FPQ.db"];
char *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:dbPathString]) {
const char *dbPath = [dbPathString UTF8String];
//create db
if (sqlite3_open(dbPath, &fpqDB)==SQLITE_OK) {
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS FPQ (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, CHECK INTEGER, COMMENTS TEXT)";
sqlite3_exec(fpqDB, sql_stmt, NULL, NULL, &error);
sqlite3_close(fpqDB);
}
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Actions
- (IBAction)done:(id)sender
{
[self.delegate flipsideViewControllerDidFinish:self];}
- (IBAction)deleteEntry:(id)sender {
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrayOfCheck count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
FPQCheck *fpqCheck = [arrayOfCheck objectAtIndex:indexPath.row];
NSString *nameANDcheck = [NSString stringWithFormat:#"%#%d", fpqCheck.name, fpqCheck.check];
cell.textLabel.text = nameANDcheck;
cell.detailTextLabel.text = fpqCheck.comments;
return cell;
}
#end
You have mainly two ways:
add a property (eg. self.flipSideController) to your MainViewController to store a reference to the FlipsideViewController; then call SaveFPQData though it (eg. [self.flipSideController SaveFPQData]; or
use notification center to post a notification from saveButton: that triggers SaveFPQData; this would go like this:
//-- in flipsidecontroller `viewDidLoad`:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(SaveFPQData)
name:#"SaveFPQData"
object:nil];
//-- in saveButton:
[[NSNotificationCenter defaultCenter] postNotificationName:#"SaveFPQData" object:nil];
The second method is the simplest to implement, IMO, and it allows for the loosest coupling, at the expense of some clock cycles.
EDIT:
It is not entirely clear to me what you are trying to do (specifically, I don't understand fully how you can push the button in MainViewController once you FlipsideViewController is displayed; on the other hand, if you do not segue to the FlipsideViewController, then it is not there, so you cannot send a message to it), anyway you could try and initialise your self.flipsideViewController property in prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showAlternate"]) {
UIViewController* controller = [segue destinationViewController];
[controller setDelegate:self];
if ([controller isKindOfClass:[FlipsideViewController class]])
self.flipsideViewController = (id)controller;
}
}
after doing that, your MainViewController will be able to send the saveFPQ message to the FlipsideViewController.
If you mean you would like to send the saveFPQ message before segue-ing to the FlipsideViewController, you should make the saveButton segue to it and the call the saveFPQ method.
What I suspect is you need some kind of "model" object accessible both from the main view and the flipside view controller.
Hope this helps.
The idea is that I hava a custom view where the user can drag and drop one or more files and the controller is able to save the path of files into an array.
How can I run a method from AppDelegate after the user drops the file in the interface?
I have these files:
AppDelegate.h:
#import <Cocoa/Cocoa.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSScrollView *table;
#property (assign) IBOutlet NSWindow *window;
#end
AppDelegate.m:
#import "AppDelegate.h"
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
#end
DropView.h:
#import <Cocoa/Cocoa.h>
#interface DropView : NSView <NSDraggingDestination>
#property (assign) IBOutlet NSScrollView *table;
#property NSArray *draggedFilePaths;
#end
DropView.m:
#import "DropView.h"
#implementation DropView
#synthesize draggedFilePaths;
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self registerForDraggedTypes:[NSArray arrayWithObject:NSURLPboardType]];
}
return self;
}
-(NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender{
return NSDragOperationGeneric;
}
-(NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender{
return NSDragOperationCopy;
}
-(BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender{
return YES;
}
-(BOOL)performDragOperation:(id<NSDraggingInfo>)sender{
NSPasteboard* prb;
prb= [sender draggingPasteboard];
draggedFilePaths = [prb propertyListForType:NSFilenamesPboardType];
return YES;
}
- (void)concludeDragOperation:(id<NSDraggingInfo>)sender{
[self setNeedsDisplay:YES];
NSLog(#"path %#",draggedFilePaths);
[self populateTable];
}
- (void)drawRect:(NSRect)dirtyRect
{
}
-(void)populateTable{
NSLog(#"yes");
}
#end
Import AppDelegate.h into DropView.m, and call the method you want to run from the performDragOperation: method.
-(BOOL)performDragOperation:(id<NSDraggingInfo>)sender{
NSPasteboard* prb;
prb= [sender draggingPasteboard];
draggedFilePaths = [prb propertyListForType:NSFilenamesPboardType];
[(AppDelegate *)[[NSApplication sharedApplication]delegate] doWhatever:draggedFilePaths];
return YES;
}
Where doWhatever: is a method implemented in the app delegate.