Can't Get AssetType set in DetailView To First Screen


#1

I need help to solve this problem I’m trying to get the contents of the asset picker (e.g. Type: Furniture) in the DetailView to show on the ItemsViewContoller. I have created a new UIButton (also tried UILabel) in HomepwnerItemCell called assetLabel to do this. To make room I’m going to remove the serial number from the ItemsView.

So when I pick “Furniture” in the DetailViewContoller.xib, I want to see it in ItemsViewController.xib; the views that is.

Our set up is in DetailViewController and this shows the asset type in viewWillAppear:

NSString *typeLabel = [[item assetType] valueForKey:@“label”];
if(!typeLabel)
typeLabel = @“None”;
[assetTypeButton setTitle:[NSString stringWithFormat:@“Type: %@”, typeLabel]
forState:UIControlStateNormal];

I’ve tried three ways to get this work, but nothing works for me. I believe it should be do-able as the details already show in our DetailView.

  1. I tried creating a a label in HomepwnerItemCell.xib but found that didn’t work so I set up a UIButton.

I tried passing in the value typeLabel into the cell

[[cell assetTypeLabel] setText:[p typeLabel]];

but the compiler says there is No Visible @interface.

  1. I then tried using the code from DetailViewController and put this in ItemsViewController but TypeLabel keeps returning “none” and with other adjustments I got “null” as I can’t get it to work with the selected cell “p”, in cellForRowAtIndexPath:

BNRItem *p = [[[BNRItemStore defaultStore] allItems]
objectAtIndex:[indexPath row]];
HomepwnerItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@“HomepwnerItemCell”];

  1. I then tried to use viewWithTag to set the field

UILabel *b = (UILabel *)[self.view.window viewWithTag:20];
[cell assetTypeLabel] setText: [NSString stringWithFormat: @"%d", b];

This also doesn’t work for me. I also tried a UIView and UIButtonView version of this as well but nothing worked.

Can someone suggest a solution or a method to solve this? I’m sure there must be an easy way to do this but I can’t see the solution at present.

Thanks in advance
Diggerbyte


#2

If I am reading correctly, I believe you are trying to do something like this:

First you want a button in your customer UITableViewCell…

Which will display AssetTypePicker…

Which as you would expect, on selecting a different asset type, will display this in the DetailView (here I selected Jewelry)…

If so, then it’s pretty straight forward, as the code you need is pretty much already there.

If you remember, in the chapter we allowed iPad users to tap on the thumbnail, which would open up a popover.

To implement the above, all I did was create a new IBAction method called:

and implemented it like so…

[code]- (IBAction)chooseAssetType:(id)sender
{
// Get the name of this methid, "chooseAssetType"
NSString *selector = NSStringFromSelector(_cmd);

// selector is now "chooseAssetType:atIndexPath:"
selector = [selector stringByAppendingString:@"atIndexPath:"];

// Prepare a selector from this string
SEL newSelector = NSSelectorFromString(selector);

NSIndexPath *indexPath = [[self tableView] indexPathForCell:self];

// Check to see if the controller implements showImage:atIndexPath:
if (indexPath)
{
    if ([[self controller] respondsToSelector:newSelector])
    {
        [[self controller] performSelector:newSelector 
                                withObject:sender // The UITableViewCell (HomepwnerItemCell)
                                withObject:indexPath];
    }
}

}[/code]

Then in ItemsViewController I sent the message to the above method by doing:

[code]- (void)chooseAssetType:(id)sender atIndexPath:(NSIndexPath *)ip
{
// get the item for the index path
BNRItem *i = [[[BNRItemStore sharedStore] allItems] objectAtIndex:[ip row]];

AssetTypePicker *assetTypePicker = [[AssetTypePicker alloc] init];
[assetTypePicker setItem:i];

[[self navigationController] pushViewController:assetTypePicker animated:YES];

}[/code]

Is this what you are trying to achieve?

Of course, if you want to display the chosen asset type in ItemsViewController, then simply added another label, and set it in the implementation.

Nick
http://myfirstiphoneapp.co.uk
http://easyintervalsapp.co.uk


#3

Hi Fujilla

That is a great solution. I’m sorry but I can see that I have not expressed my problem correctly. I’ve just re-read what I have said in describing the problem and can see that I need to further clarify the problem - apologies for that.

Using your example, what I want to do is get a copy or whatever is selected on the iMac screen (DetailViewController), in this case: Type: Jewelry and show it in the HomePwner view (ItemsViewContoller). So I am displaying the chosen asset type in ItemViewController that was picked in the DetailViewController.

I have tried creating a new label (tried both UIButton and UILabel) but I can’t get to set in in the implementation.

I’ve tried things in the DetailViewController in ViewWillAppear where we set the fields for the ItemsViewController such as:

[typeLabel setText:[NSString stringWithFormat:@"%d", [item assetTypeLabel]]];

This produces an error: No visible@interface for “BRNItem” declares the selector “assetTypeLabel”.

Thanks again
Diggerbyte


#4

Ah, I see, all you want to do is display the AssetType in the table cells in ItemsViewController. I must have completely misread that as you were talking about using UIButtons etc…

Anyway, all you have to do is copy the code from DetailViewController into cellForRowAtIndexPath in ItemsViewController:

NSString *typeLabel = [[p assetType] valueForKey:@"label"]; if (!typeLabel) typeLabel = @"None";

Then set your new label something like:

Nick
http://myfirstiphoneapp.co.uk
http://easyintervalsapp.co.uk


#5

Hi Fujilla

Thanks for that. I really appreciate your help again. I set the label using:

[[cell assetLabel] setTitle:[NSString stringWithFormat:@"Type: %@", typeLabel] forState:UIControlStateNormal];

As assetLabel is a UIButton. In my work I had the cell reference “p” in the wrong part of the code but I was so muddled up anyway.

You didn’t mis-read anything, I went back and rewrote the problem statement. I felt so bad and guilty that you did all that work and I did not properly state what I was doing and state exactly what the problem was. I’ll be more careful next time and check my problem statement.

Thanks again
Diggerbyte


#6

You are welcome.

I am however wondering - why are you displaying the Asset Type in a UIButton on ItemsViewController, when it sounds like you are not actually using the button as a “button”?


#7

Your’re right it shouldn’t be a button. I did that because the other button was a UIButton and as I could get anything to work I set a UIButton. I had better go back and change it and put a UILabel in its place.


#8

Ah, OK.

Keep it as a button if you like :wink:, however for future reference - If you were developing an app for the app store, this may cause the app to be rejected.


#9

Thanks, didn’t know that.

Do you know if Apple would reject the yellow lamp warning: “PerformSelector may cause a leak because its selector is unknown.” in HomepwnerItemCell.m?

[code]- (IBAction)showImage:(id)sender
{
NSString *selector = NSStringFromSelector(_cmd);
selector = [selector stringByAppendingString:@“atIndexPath:”];
SEL newSelector = NSSelectorFromString(selector);

NSIndexPath *indexPath = [[self tableView] indexPathForCell: self];

if(indexPath) {
    if([controller respondsToSelector:newSelector]) {
          [controller performSelector:newSelector  //warning here "PerformSelector may cause a leak because its selector is unknown"
                           withObject:sender 
                           withObject:indexPath];
    }
}

}
[/code]

Going to try sort the assets now in ItemsViewController. Have you done this before?


#10

The way I see it is that one should try and not have any warnings in their code, and is why I am confused as to why the authors have us write code which causes a warning, and say simply ignore it.

I don’t believe apple would reject an app for the odd warning, as long as it didn’t cause any problems at runtime.

No, I haven’t sorted the cells in ItemsViewComtroller by asset type, however am guessing it is similar to the way that has already been discussed.

Nick