UITextField not setting NSString - nsstring

I have a pickerView in a popOver.
When the user selects a row from the pickerView, it should be stored into a UITextField (subject)
The method i'm having troubles is:
- (void) viewWithPickerController:(ViewWithPickerController*) viewWithPickerController didSelectValue:(NSString*) value
{ NSLog(#"selected value is: %#",value);
subject.text = [NSString stringWithFormat:#"%#",value];
NSLog(#"subject is: %#",subject.text);
}
Where value is the row of the pickerView: it logs the right value but it doesn't set it to the textField subject, which seems to be null.
Where am i wrong?

You need to make sure your "subject" IBOutlet is set.
If you're doing this picker view method before your XIB / view controller is instantiated, that would explain why subject is a nil object.
How else would you be instantiating "subject"?

Related

Can't programmatically bind NSImageView in view-based NSTablveView cellView

I have a view-based NSTableView which is a single column with a custom CellView containing about a dozen text fields and a single NSImageView.
-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
ComboTableCellView* view = [tableView makeViewWithIdentifier:[tableColumn identifier] owner:nil];
// I bind the text fields with:
[[view nameView] bind:NSValueBinding toObject:view withKeyPath:#"objectValue.name" options:nil];
// I bind the NSImageView with:
NSMutableDictionary* statusOptions = [[[NSMutableDictionary alloc] initWithCapacity:0] autorelease];
[options setObject:kStatusIconTransformerName forKey:NSValueTransformerNameBindingOption];
[[view statusView] bind:NSValueBinding toObject:view withKeyPath:#"objectValue.status" options:statusOptions];
return (view);
}
My transformer takes "status" which is an NSNumber and returns an NSImage because status will be a number from 1 to 4. This works in a cell based NSTableView with an NSImage column.
However, in my view-based NSTableView I get:
Cannot remove an observer (NSAutounbinderObservance 0x600000028ac0) for the key path "objectValue.status" from (ComboTableCellView 0x6180001e3e00), most likely because the value for the key "objectValue" has changed without an appropriate KVO notification being sent. Check the KVO-compliance of the ComboTableCellView class.
How can I correctly bind an NSImageView that is within a reuseable view used as the row/cellview in a view-based NStableView?

NSTableView with images

I have NSTableView with 3 tabs.
Now, I want an image inside every tab, how can i do ?
I must use NSImage with position, so ?
NSImage *theImage;
theImage = [NSImage imageNamed#"myImage.png"];
you can do like this:
in code create one NSMutableArray with property and synthesize
header file:
#property (readwrite, retain) NSMutableArray *imageArray
implementation file:
#synthesize imageArray
do like this wherever you want to add image to array:
NSMutableDictionary *imageDict = [NSMutableDictionary dictionary];
[imageDict setObject:[NSImage imageNamed:#"ABC"] forKey:#"image1"];
[imageDict setObject:[NSImage imageNamed:#"XYZ"] forKey:#"image2"];
[imageDict setObject:[NSImage imageNamed:#"XYZ"] forKey:#"image3"];
[imageArray imageDict];
[self setImageArray:imageArray];
take array controller in XIb, bind arraycontroller to imageArray array, bind table column with the array controller with given key path (here keypath is image1,image2 and image3).
one More thing take image name without extension . drag and drop image cell on tableview cell

How to identify previous ViewController loaded in IF statement

I have an NSString called stringToDisplay, in which i would like to assign a different value if a certain ViewController was visited. How could I set this up? Is this even possible? ...example of what I want:
If ([prevViewController.visited] == Yes)
{
stringToDisplay = value2;
}
Assuming you are using a navigation controller, you can just do:
NSArray *views = self.navigationController.viewControllers;
UIViewController *previous = [views objectAtIndex:[views count]-2];
where [views objectAtIndex:[views count]-1] is the current ViewController;

cellForRowAtIndexPath: returns nil for a generic prototype cell

This is driving me nuts!
I have a generic UITableViewController class with a generic prototype cell, with an identifier "myCell". Developing under ARC, iOS5 and using Storyboard, I am using the following method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
cell.textLabel.text = #"Title";
return cell;
}
Everything is hooked up in the storyboard, file owner, etc. I have several other UITableViewController in my project, using the same concept. All are working.
This particular class of UITableViewController doesn't want to work! keep throwing me the exception:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
Nslog'ed --> [tableView dequeueReusableCellWithIdentifier:#"myCell"] = (null)
Any idea and help is greatly appreciated!
EDIT:
For simplicity:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
Things that I have already done:
1. delete and create and set up the UITableViewController in storyboard!
2. Restarted Xcode
3. Restarted simulator
4. Restarted computer!!!!
5. Deleted and built the app over and over!
None worked.
By the way, I tried
if (cell == nil)
cell = [UITableViewCell alloc] initWithStyle....
.. and it will solve the problem, but again... this is ARC, storyboard, ... I am not supposed to do that. Xcode is supposed to take care of it!
You have to create your cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"myCell"] autorelease];
}
cell.textLabel.text = #"Title";
return cell;
}
Eventually I found the bug. I can't say I found the root cause, but investigating line by line, this is what I found and worked for me.
I have some objects in my UITableViewController that need to be alloc/init'ed before the view gets loaded, so that when callers can set them to pre-determind values. Since viewDidLoad is too late, I put them in initWithCoder method.
Commeting out and re-writing the initwithCoder method solved the problem. It seemed to me, that initWithCoder method was initing the UITableViewController as some different!
I had the same problem and just managed to overcome it.
If you are creating cells data dynamically you have to do the following things:
Select the table view.
Then go to the object inspector to the third tab from the right.
The first option is named: "Content". Change the selection from "Static Cells" to "Dynamic Prototypes".
Make sure you set the identifier of the cell properly and it is the same name you use on the "dequeueResableCellWithIdentifier"
It worked for me. I stopped getting "nil" cell values and everything seems to work properly.
if the "IF" statement is falling to true (cell == nil) for the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"myCell"] autorelease];
}
cell.textLabel.text = #"Title";
return cell;
}
then the name #"myCell" was either misspelled (Doesn't match) or is missing from the storyboard identifier field for the cell.

Please explain me how to control a Table View in Cocoa?

I search Google to find a good answer but the most tutorials are shown in previous Xcode Versions...
Also, I don't want to drag-n-drop cells from the Interface Builder, but to control the Table View programmatically (from an NSObject subclass file).
What I currently do is this: 1. Create a file named tableController.h that is a subclass of NSObject.
2. I create an NSObject Object in my Nib File (and set it as a subclass of tableController).
3. I drag a Table View to my window.
4. I CTRL+Drag from the Table View to my tableController.h so to create the outlet "tableView"
5. I create these functions in the interface file:
-(int)numberOfRowsInTableView:(NSTableView *)cocoaTV;
-(id)tableView:(NSTableView *)cocoaTV:objectValueForTableCollumn:(NSTableColumn *)tableCollumn row:(int)row;
6. I implement the functions like this:
-(int)numberOfRowsInTableView:(NSTableView *)cocoaTV{
return 5;
}
-(id)tableView:(NSTableView *)cocoaTV:objectValueForTableCollumn:(NSTableColumn *)tableCollumn row:(int)row{
NSArray *tvArray = [[NSArray alloc]initWithObjects:#"1",#"2",#"3",#"4",#"5", nil];
NSString *v = [tvArray objectAtIndex:row];
return v;
}
Then I CTRL+Drag from the Object in the Interface Builder to the Table View to set the dataSource and to set it as delegate.
When I build and Run the App it shows that it has created the 5 Rows but in every cell in every column it says "Table View Cell".
Any help would be appreciated....
-(id)tableView:(NSTableView *)cocoaTV:objectValueForTableCollumn:(NSTableColumn *)tableCollumn row:(int)row is wrong.. i'm not sure how it compiles, to be honest (unless there was an error copy/pasting it). the method should look like:
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSArray *tvArray = [[NSArray alloc]initWithObjects:#"1",#"2",#"3",#"4",#"5", nil];
NSString *v = [tvArray objectAtIndex:row];
return v;
}

Resources