Initialise components with custom names - objective-c

I have a view that contains 15 images and labels .
I ve put them all on view but in some cases I don t want to show them all .
For example I have
IBOutlet UIImageView* image1 ;
....................* image2 ;
......................
.....................*imagen;
I have a list with objects but if i don t have n objects and i have just m i don t want to show images from m to n.
In for loop I would like to have something
- > string componentToShow = "image" + i ;
and now (componentToShow).....set to visible and diffrent prop .
Thanks,
Raluca

If I understand you correctly, you've got a varying number of objects, and you want to display accompanying views for the objects you have. You have laid out the maximum number of accompanying views in a nib file, and are now struggling with hiding the ones you don't want. Is that correct?
I would do it like this:
Take the accompanying views out of the nib. Create a second nib that represents a single object (e.g. label and image together). In your view controller's viewDidLoad method, load the nib representing an object and store it in an instance variable. When an event happens that creates a new object, instantiate the nib and add the views produced to your view controller's view hierarchy.
Alternatively, if your representation of objects is simple enough, skip the second nib, and simply create the views directly in code.

Related

NSOutlineView parentForItem returns nil when parent is collapsed

I have an NSOutlineView for which I have written the datasource and delegate. When I add an item to a group and the group item is in the collapsed state, parentForItem returns nil.
Here is the code I used to test this.
- (IBAction)addItemToGroup:(id)sender {
TLItem *theNewItem= [[TLItem alloc] initWithTitle:#"My New Item"];
TLItem *theGroupItem = [self.sourceListItems objectAtIndex:0];
NSMutableArray *theItemList = [theGroupItem children];
[theItemList addObject:theNewItem];
[self.sourceListOutlineView reloadData];
TLItem *newItemParent = [self.sourceListOutlineView parentForItem:theNewItem];
NSLog(#"newItemParent = %#", newItemParent);
}
If theGroupItem is expanded, this method logs this:
newItemParent = TLItem: 0x60800004bbb0
If theGroupItem is collapsed, this method logs this:
newItemParent = (null)
How can I get the parent for the newly added item?
NOTE: I realize this is a silly example, but in my actual code, I need to be able to walk up the tree to find all parents in the hierarchy.
Given that you have written the data source, use that, not the outline view, to navigate the hierarchy. The data source is the authority on the model. The outline view is just a means of presenting some portion of that model. The outline view will not have a complete representation of the model in the general case, and would often need to consult the data source to answer such queries, anyway. So, skip the middle man and go straight to the source, as it were.
If necessary, your item class should have a weak reference to its parent. All operations on the hierarchy (adding children, removing children, moving items from one parent to another, etc.) should go through dedicated methods on the data source which keep that parent reference updated. You should not simply obtain and modify a mutable array of an item's children outside of such hierarchy-manipulation-specific methods.

NSTableView + NSTextView = Disaster :(

This is driving me a bit crazy..
Down below is a screenshot of my program so far.
On the right is an NSTableView (view-based). This is where the user can select a document they want to work on.
On the left is the NSTextView. Text will be displayed depending on what item they choose in the NSTableView.
There are also big plus and minus buttons for creating/deleting new items in the tableview.
Simple right? I wish.
Right now I have it so the tableview gets data from a mutable array. The mutable array contains objects of a class called DocumentItem. The DocumentItem just has two strings, one for the document text and one for the document title.
What works so far:
When I manually add objects to the array using code, I am able to freely switch through the documents and the textview will update accordingly.
What doesn't work:
When the user switches to a different document, I want to call the NSTableView replaceObjectAtIndex method save the changes that they have made to the object in the array.
How my code works so far:
The mutable array is stored in a data class. The data class is a shared class and is referred to in my code as theDATA.
I have a thread looping in my class that has the textview. In my tableview class I have a method called blastToScreen that will change a BOOL called shouldBLAST to YES.
Here is the code in my TableController class to set the BOOL to YES:
- (void) blastToScreen{
theDATA.blasttext = [[theDATA.globaldoclist objectAtIndex:[tablevieww selectedRow]] doccontents];
theDATA.shouldBLAST=YES;
}
Here is the shouldBLAST method in my looped thread(in a different class from the textview). Please note that the if-statement that says if(theDATA.switchedrow) is there to make sure that certain code gets runned only when a user switches their row in the tableview.
if(theDATA.shouldBLAST){
if(theDATA.switchedrow){
DocumentItem * itemr = [theDATA.globaldoclist objectAtIndex:theDATA.lastindex];
NSLog(#"(%li) prev content - >%#",(long)theDATA.lastindex,itemr.doccontents);
itemr.doccontents=textvieww.string;
NSLog(#"(%li)adding content - > %# <- to %#",theDATA.lastindex, itemr.doccontents,itemr.docname);
theDATA.switchedrow=NO;
[theDATA.globaldoclist replaceObjectAtIndex:theDATA.lastindex withObject:itemr ];
NSLog(#"changed: - > %#",[[theDATA.globaldoclist objectAtIndex:theDATA.lastindex] doccontents]);
}
textvieww.string=theDATA.blasttext;
theDATA.shouldBLAST=NO;
NSLog(#"changed: - > %#",[[theDATA.globaldoclist objectAtIndex:theDATA.lastindex] doccontents]);
theDATA.lastindex=theDATA.selectedrow;
}
Here's the weird part about all this:
According to the NSLog statements I set up, my code works for a split second and then resets.
Down below is what the console says. ignore the (0). that is just talking about the last selected index.
What it is saying is that the text before switching was nothing(fine). It is saying that it is adding the text "Potato" to that array(still fine). Then, the first time I fetched the object from the array it shows that it successfully changed to "Potato"(Still fine). Then when I tried to fetch the SAME exact data a few lines later, it returned nothing. :(
I feel like the issue resides somewhere in my TableController class. Here's a link to the code in my TableController class.
Here's what the console returned:
2015-09-14 17:17:46.024 Simplicity[4801:432580] (0) prev content - >
2015-09-14 17:17:46.025 Simplicity[4801:432580] (0)adding content - > Potato <- to Untitled
2015-09-14 17:17:46.025 Simplicity[4801:432580] changed: - > Potato
2015-09-14 17:17:46.025 Simplicity[4801:432580] changed: - >
I really hope you guys can help me. I tried pretty much everything I could to solve this issue.This is holding me back from finishing my software.
Probably, the doccontents property of your DocumentItem class is strong (or retain) when it should be copy.
From the docs for the string property of NSText (from which NSTextView inherits):
For performance reasons, this method returns the current backing store of the text object. If you want to maintain a snapshot of this as you manipulate the text storage, you should make a copy of the appropriate substring.
So, if you're just keeping a reference to that same object, when the text view's content is changed, the content of the object you've got a reference to also changes. You need to make a private copy.

How to bind different entities with NSTableView and NSTabView?

I have an NSArrayController which handles entities of GeometryShape.
GeometryShape has: name, type, color.
LineShape is a GeometryShape and has: beginPositionX, beginPositionY, endPositionX, endPositionY.
CircleShape is a GeometryShape and has: positionX, positionY, radius.
The NSTableView shows all inserted shapes in the NSArrayController, where each column is bound with arrangedObjects & the key name.
When I select a line shape, its properties are displayed in the Line tab - which is the default tab.
Now if I select a circle shape, I want the Circle tab to be selected and the circles properties to be displayed.
…and so depending on which shape type I select the corresponding tab will be selected and display the corresponding shape properties.
How may I achieve this excellent :) model?
I think you'd want to implement a NSTableViewDelegate and programmatically select the appropriate tab within an implementation of tableViewSelectionDidChange: When the selection changes, you just grab the tabView's IBOutlet and assign a new selectedIndex based on the arrayController's selection.
Alternately, you could bind the value of the tabView's selectedIndex to the array controller's selection, but you would need a custom value transformer that converted from the selection id to an NSUInteger that reflects the appropriate class.
In either implementation, you're writing code using isKindOfClass and mapping to an integer.
You may also be able to bind the tab view's selectedLabel to the array controller keypath of selection.class but I'm guessing you still would need a valuetransformer wrapping NSStringFromClass() as described in the NSValueTransformer docs. I'm not totally certain there's a completely non-code way of transforming the class to a string you could bind the selectedLabel to, though.
Personally, I don't love implementing the custom value transformer because you're writing code to allow implementing behavior buried in IB... all to avoid writing code that could live in a custom tableview delegate.

How do I determine the row in NSTableView that represents a specific object instance?

I have a NSTableView driven by NSManagedObject/CoreData and a custom graph view on the same form which plots each row on a X/Y grid based on some derived calculations. Each point on the graph view causes and event to run a (SEL)Selector on my window controller..
And I am stuck ! I want to be able to change the selection in the NSTableView to be the object selected and returned by the graph view. ie. use the graph to change the selection/row in the NSTableView list.
I have a method as follows:
-(void)setSelectedOpportunity:(Opportunity *)opty
{
_selectedOpportunity = opty;
[_opportunityTable selectRowIndexes:???? byExtendingSelection:NO];
}
_opportunityTable is an NSTableView that gets is content from and NSArrayController called _opportunityAC. This array controller gets its data from a NSManagedObjectContext containing NSManaged objects called Opportunity.
Can anyone tell me how do I determine the row in the table that represents the object opty.
Thanks
Ian

organize data model objective c

I have this visual part in my app. I've attached file below:
There are next user interface controls:
First UIScrollView (_blueScrollView) is located on main view of UIViewController that can contain many other views each one is called _magentaView...
Each _magentaView contains UIView (_labelView...) for storing UILabel objects. So there is some struct like UITableView in UITableView.
In my case all objects have UIViewController class.
I need to get access to all my labels that are storing in labelsViews. I need to get value of property text of those objects.
So if I make in standard case this will looks like this
for (UIViewController *magentaVC in scrollViewObjects) {
for (UIViewController *labelVC in magentaObjects) {
and get my UILabel objects here.
}
}
but as I think this is incorrect. Because I need to use data model for this instead to use UIViewController objects. What do you think about this and which way do I need to choose?
This is exactly the job for a UITableView. The table itself has a scroll view embedded in it; magentaView 1 and 2 would be sections of the table, and the labelViews would be table view cells.