UA-17470720-3

Jump to content


Photo
* * * - - 2 votes

Intercept '+' add button


  • Please log in to reply
13 replies to this topic

#1 gayraud

gayraud

    Jr. Member

  • STV 5.0 Pro
  • PipPip
  • 28 posts
Reputation: 0
Neutral

Posted 06 February 2013 - 04:53 AM

Hi everyone,

I would like to intercept the ADD button to display an UIAlertView for example "are you sure you want to add a new element? yes/no" ?

if yes -> continue the regular process (opening a new tableview)
if no -> do not open the new tableview and stay on the current model view

I found that the '+' is firing up the following method:

- (void)tableViewModel:(SCTableViewModel *)tableViewModel itemCreatedForSectionAtIndex:(NSUInteger)index item:(NSObject *)item

But I am not sure how to PREVENT the form to be displayed.

Is there a way ?

Edited by gayraud, 06 February 2013 - 04:55 AM.


#2 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 06 February 2013 - 09:03 PM

Hi gayraud,

All you need is handle the '+' button action yourself, then call the SCArrayOfObjectsSection method called 'dispatchEventAddNewItem' whenever you need to display the new item detail view. Here is a code sample of how to do this:

- (void)viewDidLoad
{
  ...
  self.navigationBarType = SCNavigationBarTypeAddButtonRight;
  self.addButton.target = self;
  self.addButton.action = @selector(didTapAddButton); 
  ...
}

- (void)didTapAddButton
{
  /* Your conditional code here */

  if(myCondition)
  {
     // Assuming SCArrayOfObjectsSection is the first section
     SCArrayOfObjectsSection *myObjectsSection = (SCArrayOfObjectsSection *)[self.tableViewModel sectionAtIndex:0];
     [myObjectsSection dispatchEventAddNewItem];
  }
  else
  {
     // take alternative action here
  }
}

Hope this helps!
  • wizgod likes this

#3 gayraud

gayraud

    Jr. Member

  • STV 5.0 Pro
  • PipPip
  • 28 posts
Reputation: 0
Neutral

Posted 07 February 2013 - 02:44 AM

Thanks Tarek!
It's working like a charm!

Edited by gayraud, 07 February 2013 - 05:22 AM.


#4 ozie

ozie

    ¯\_(ツ)_/¯

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 526 posts
  • LocationAustralia
Reputation: 169
Popular

Posted 03 May 2013 - 07:39 PM

Tarek.. i have the same need, and just tried this and the event didTapAddButton never fires, but the detailview is still pushed
i even tried setting the code with the SCArrayOfObjectsSection
objectsSection.addButtonItem.target = self;
objectsSection.addButtonItem.action = @selector(didTapAddButton);

and that didnt fire the event either

is there something i need to do to get the add button to fire my custom event?

**EDIT
i just created my own button and added that.. its doing what i need it to do thats the main point:)
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(didTapAddButton)];
self.navigationItem.rightBarButtonItem = btn;
Oz

Edited by ozie, 03 May 2013 - 07:42 PM.

P.S. I hate Swift.. don't talk Swift.. Too old school to learn yet another programming language.


#5 alphaai

alphaai

    Sr. Member

  • STV 3.0 Pro
  • PipPipPip
  • 41 posts
Reputation: 11
Good

Posted 02 June 2013 - 05:28 PM

Hey Tarek,

 

Works great.

Ok. I have a UIActionSheet that popups upon didTapAddButton event. I have selected an item in my UIActionSheet and based on the selection I chose some of the detailView cells will have to be hidden and some must be visible. How do I do that?

Thanks,

 

Ben


Edited by alphaai, 02 June 2013 - 05:41 PM.


#6 alphaai

alphaai

    Sr. Member

  • STV 3.0 Pro
  • PipPipPip
  • 41 posts
Reputation: 11
Good

Posted 02 June 2013 - 05:39 PM

Hey there again Tarek,

 

Once item was selected, I tried to continue dispatchEventAddNewItem

 

Instead of SCArrayOfObjectsSection as the first section

I manually added mine with SCTableViewSection.

 

I tried:

 

SCTableViewSection *myFirstSection = (SCTableViewSection *)[self.tableViewModel sectionAtIndex:0];

[myFirstSection dispatchEventAddNewItem];

 

and that didnt work.


Edited by alphaai, 02 June 2013 - 05:43 PM.


#7 Everett

Everett

    Ev

  • STV 5.0 Pro
  • PipPipPipPipPip
  • 191 posts
  • LocationMichigan
Reputation: 60
Outstanding

Posted 04 June 2013 - 06:31 PM

Hi Alphaai,

 

did you get this worked out?  

 

As you noted, dispatchEventAddNewItem will not work with SCTableViewSection.  It's first implement in SCArrayOfItemsSection and meant for the object section subclasses that generate the views for adding, deleting, etc.

 

What type of data are you displaying in your first section?  Could you simply move it to an SCArrayOfObjectsSection?  Or possibly move your entire table to an SCArrayOfObjectsModel?

 

In either of those cases you would be able to create a workflow where you can cause your own method to run before a framework method.

 

best of luck



#8 alphaai

alphaai

    Sr. Member

  • STV 3.0 Pro
  • PipPipPip
  • 41 posts
Reputation: 11
Good

Posted 04 June 2013 - 11:56 PM

Hey there Everett

 

No I haven't made much progress as yet.

 

As mentioned in this post http://sensiblecocoa...st-of-subtasks/
, I can show one task and allow one to view the rest of the cells associated to that section or heading by clicking on the DetailDisclosureButton.

 

I do not want to do generating any cells as such and I think that is what SCArrayOfObjectsSection will do. So I believe SCTableSection seem appropriate that way I can add the cells manually which is where SCArrayOfObjectsCells come in.



#9 Everett

Everett

    Ev

  • STV 5.0 Pro
  • PipPipPipPipPip
  • 191 posts
  • LocationMichigan
Reputation: 60
Outstanding

Posted 05 June 2013 - 07:57 AM

Hi Alphaai,

 

I read your other posts and your right, you may want to create a post where you more fully explain what you are trying to accomplish.  We have some folks here that want to help, but our advice can be all over the place depending on how well we understand your goals.

 

 A quick question related to your design here.  You want show one entry (the oldest entry) (what do you want to show i.e. title or more info?) then when the detail disclosure is tapped, show the complete list.

 

What do you want to happen when the cell is tapped?  When you enable the detail disclosure, you now have two paths - one for the tableview cell and one for the detail disclosure?  Could you explain what you want to accomplish in each path?

 

or maybe do that in a new post with all the details together :)



#10 alphaai

alphaai

    Sr. Member

  • STV 3.0 Pro
  • PipPipPip
  • 41 posts
Reputation: 11
Good

Posted 06 June 2013 - 08:15 PM

Hey Everett,

 

To the left of the cell will be an icon, click it and the task (cell) disappears. eg. completed.

If I click instead click on detail disclosure, it will show up the full list.

If I click on the cell instead, the detailView shows up.

 

The other way I could do it is to allow the user to click on the heading and that takes you to the full list

but who clicks on headings??

 

Ben



#11 alphaai

alphaai

    Sr. Member

  • STV 3.0 Pro
  • PipPipPip
  • 41 posts
Reputation: 11
Good

Posted 06 June 2013 - 08:23 PM

Hey Everett,

 

I did try    

 

SCArrayOfObjectsSection *mySection = [SCArrayOfObjectsSection sectionWithHeaderTitle:@"Rewards" entityDefinition:RewardsDef];
   // [mySection dispatchEventAddNewItem];
    
    [self.tableViewModel addSection:mySection];

 

however I lose the items badge count of how many cells there are in the full list and I also lose the "see one cell on main view, see the rest in next view"

 

Haven't tried SCArrayOfObjectsModel.

 

Ben



#12 Everett

Everett

    Ev

  • STV 5.0 Pro
  • PipPipPipPipPip
  • 191 posts
  • LocationMichigan
Reputation: 60
Outstanding

Posted 07 June 2013 - 12:31 PM

Hi Ben,

 

I think I understand what your shooting for.  Here is a quick prototype that presents an arrayOfObjectCell.  This will give you the count of items and I've included a sample of how to modify it to show details of the oldest data item.  

 

It provides a detail disclosure item.

 

If you tap on the cell, it takes you to the list of items.

If you tap on the detail disclosure, it will give you just the contents of the cell.

 

You should be able to just drop it in your project and call it.

 

Hope this helps!

Everett

 

 

 

 

- (void)prototypeForBen {

    

    __weak typeof(self) blockSelf = self;

    

    //create simple data model for example

    NSMutableArray *exampleData = [NSMutableArray new];

    for (int i = 1; i <= 10; i++) {

        NSString *item = [NSString stringWithFormat:@"Item Number:%d",i];

        NSString *fakeCount = [NSString stringWithFormat:@"%u",i * arc4random()];

        NSMutableDictionary *dict = [NSMutableDictionary new];

        [dict setObject:item forKey:@"name"];

        [dict setObject:fakeCount forKey:@"fakeCount"];

        [exampleData addObject:dict];

    }

    

    SCDictionaryDefinition *dictDef = [SCDictionaryDefinition definitionWithDictionaryKeyNamesString:@"name;fakeCount"];

    

    SCArrayOfObjectsCell *cell = [SCArrayOfObjectsCell cellWithItems:exampleData itemsDefinition:dictDef];

    cell.cellActions.willDisplay = ^(SCTableViewCell *cell, NSIndexPath *indexPath)

    {

        SCArrayOfObjectsCell *ocell = (SCArrayOfObjectsCell*)cell;

        NSDictionary *oldItem = [exampleData lastObject];

        ocell.textLabel.text = @"Oldest Ticket";

        ocell.detailTextLabel.text = oldItem[@"name"];

        ocell.displayItemsCountInBadgeView = YES;

        ocell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

    };

 

    cell.cellActions.accessoryButtonTapped = ^(SCTableViewCell *cell, NSIndexPath *indexPath)

    {

        NSLog(@"accessory button tapped");

        

        NSDictionary *oldItemDict = [exampleData lastObject];

        SCObjectSection *section = [SCObjectSection sectionWithHeaderTitle:@"oldest ticket" boundObject:oldItemDict boundObjectDefinition:dictDef];

     

        SCTableViewController *newController = [[SCTableViewController alloc] init];

        newController.navigationBarType = SCNavigationBarTypeDoneRightCancelLeft;

        

        //when getting ready to transition back to main view, reload tableview at index path for any changed values

        newController.actions.willDisappear = ^(SCTableViewController *viewController)

        {

            [blockSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];

        };

 

        SCTableViewModel *newModel = [SCTableViewModel modelWithTableView:newController.tableView];

        [newModel addSection:section];

        newController.tableViewModel = newModel;

        

        if (blockSelf.navigationController) {

            newController.navigationItem.title = oldItemDict[@"name"];

            [blockSelf.navigationController pushViewController:newController animated:YES];

        }

        else {

            

            UINavigationController *newNavController = [[UINavigationController alloc] initWithRootViewController:newController];

            newController.title = oldItemDict[@"name"];

            [blockSelf presentViewController:newNavController animated:YES completion:nil];

        }

        

    };

 

    SCTableViewSection *section = [SCTableViewSection sectionWithHeaderTitle:@"Oldest thing from list"];

    [section addCell:cell];

    [self.tableViewModel addSection:section];

}


Edited by Everett, 07 June 2013 - 08:16 PM.


#13 alphaai

alphaai

    Sr. Member

  • STV 3.0 Pro
  • PipPipPip
  • 41 posts
Reputation: 11
Good

Posted 07 June 2013 - 09:14 PM

This is great!.

 

I have to change it over to using Core Data, i'll have a crack at it and let u know if I need help.

I am using SCCustomCell for all my cells and in particular, if I go

 

    oldestItemSection.sectionActions.cellForRowAtIndexPath = ^SCCustomCell*(SCArrayOfItemsSection *itemsSection, NSIndexPath *indexPath) {
        NSString *bindingsString = @"10:name;11:lastDateTime";
        SCCustomCell *lastTimeCell = [SCCustomCell cellWithText:nil objectBindingsString:bindingsString nibName:@"LastTimeCell"];
        
        return lastTimeCell;
    };

 

I lose access to ocell.displayItemsCountInBadgeView = YES;

Sounds like I may have to subclass SCTableViewCell and make my own custom cell which has displayItemsCountinBadgeView.

 

Also I cannot dispatchEventAddNewItem if you choose SCTableViewSection.

 

SCTableViewSection *oldestItemSection = (SCTableViewSection *)[self.tableViewModel sectionAtIndex:0];

[oldestItemSection dispatchEventAddNewItem];


Edited by alphaai, 08 June 2013 - 01:48 PM.


#14 Everett

Everett

    Ev

  • STV 5.0 Pro
  • PipPipPipPipPip
  • 191 posts
  • LocationMichigan
Reputation: 60
Outstanding

Posted 08 June 2013 - 06:02 AM

Hi Ben,

 

Your correct, if you use a custom cell, I think you'll lose the badge view because that is something STV has added for us.  

It was interesting working on this for you because I hit a couple of dead ends and had to go back and rethink how to do it.  Remember, I'm very much still learning, so don't hold it against me when Tarek comes along with an easier way :)

 

A couple of things that I stumbled on that you might find interesting and possibly odd.

 

a) I initially planned on using a SCArrayOfObjectsCell and just adding it as a "random" cell to an SCArrayOfObjectsSection.  I figured this would provide the summary cell you wanted and since we used the SCArrayOfObjectsSection we could use the dispatchEvents when the accessory button was tapped.  However, at least with dispatchEventSelectRowAtIndexPath, it turned out that it would not dispatch the SCArrayOfObjectCell that was added to the section, only cells that were in the items array provided to the method SCArrayOfObjectSection would respond.  I have a feeling this may be a bug.

 

So this was inconvienent.  I tried to build a design that worked used the generated detail view.  I had planned on using a local BOOL as a toggle in conjunction with the accessoryButton.  When it was tapped, it would dispatch and then in the detailModelConfigured, I was going to replace the tableview model with a objectSelection model instead of a object list.  First the I couldn't get the dispatch to work and after finding a work around, I couldn't change the detail model the way I planned.  

 

So instead, when the accessory is tapped, it will just generate its own detail view and present an object section.  I had the feeling that I was duplicating something the framework probably does but  my other approaches were not bearing any fruit.  Either way, because its using the framework controllers, its only a few lines of code and it seemed pretty clean.

 

I'm thinking you might be best to simply override the "add" button as described earlier in this post by Oz and present your own view, similar to the approach used for the accessory button.  If you create a good entity definition, you can have a view come up that allows adding a new record easy enough.

 

It might help if you could post a screen shot(s) of what you are trying to achieve.  I don't quite understand your data model.   In this example, you are creating a roll up cell and showing the oldest, are you rolling up groups by category or types or something.  So what would the next cell (row) be a rollup of?  Elements of the same data model but grouped by a different category?  Or is there only one cell?

 

be well!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users