[questions] SegmentedControl, NSString, Right button

Discussion in 'iOS Development' started by vieestchien, May 25, 2008.

  1. vieestchien

    vieestchien New Member

    Joined:
    May 7, 2008
    Messages:
    43
    Likes Received:
    1
    I am using toolchain to develop an application, but currently I've hit some obstacles. I have several questions and hope you guys can give me some pointers:

    1) How to determine if the user has selected a segment in a UISegmentedControl? The selectedSegment method will give segmentation fault at run time if I don't select any segment, and apparently there is not an isSelected method or something similar in the UISegmentedControl.h

    2) I want to make sure that my string only contains digits and alphabets. For now my only solution is to do a "find string within string" operation for all special characters I can think of, but it surely is not the most elegant solution out there. Anyone has a better idea? Is there a way to disable special characters in the keyboard, or to trim them all off my string?

    3) How to make the rightButton beside a UITextField to appear? I can make the clear button to appear by calling setClearButtonStyle, but setRightButtonStyle doesn't work, and setRightButton doesn't seem to exist.

    Thank you
  2. optimo

    optimo New Member

    Joined:
    Sep 28, 2007
    Messages:
    535
    Likes Received:
    17
    1> the mouseDown/Up events for segmentedControl need to be overridden. basically this means you must create your segControl as a subclassed control, and then rewrite some mouse events.
    Code:
    @interface MySegControl : UISegmentedControl
    {
    }
    - (void)mouseDown:(struct __GSEvent *)event;
    - (void)mouseUp:(struct __GSEvent *)event;
    @end
    
    @implementation MySegControl
    - (void)mouseDown:(struct __GSEvent *)event;
    {
    int button = [self selectedSegment];
    
    if (button == 1)
    {
     ///Do something
    }
    
    }
    @end

    2.> try keyboard style #4 or #8 or #9
  3. vieestchien

    vieestchien New Member

    Joined:
    May 7, 2008
    Messages:
    43
    Likes Received:
    1
    2) Style 4 works great, thanks

    Please Register or Log in to view images

    . I still have to deal with "space", but it's much easier now.
    1) The problem is, if the user doesn't press any button, the mouseUp and mouseDown functions wouldn't be called. For example, my UISegmentedControl has 2 segments: Sell and Buy. Once the user chooses the method he wants and presses Confirm, the appropriate command will be executed. But if he doesn't select any segment and goes on to press Confirm, an error will occur.
  4. vieestchien

    vieestchien New Member

    Joined:
    May 7, 2008
    Messages:
    43
    Likes Received:
    1
    Hmm, apparently I've found a solution for the 1st issue:
    Code:
    NSString *typeCommand;
    switch ([ segCtl selectedSegment ])
    {
    	case 0:	typeCommand = SELL_COMMAND; //@"Sell"
    		    break;
    	case 1: typeCommand = BUY_COMMAND; //@"Buy"
    		    break;
    }
    if (typeCommand == nil)
    {
    	[ errorSheet setBodyText: ERROR_SEG_CTL ]; //@"You have to select Buy or Sell"
    	[ errorSheet presentSheetInView: self ];
    }
    
    This is weird, because this means [ segCtl selectedSegment ] returns some int value different from 0 and 1, but I have no idea what exactly that value is. If I try to do printf([ segCtl selectedSegment ]) or NSLog([ segCtl selectedSegment ]) or something similar I would get a segmentation fault.
    Anyway, although this is not elegant, it works

    Please Register or Log in to view images

    .
  5. optimo

    optimo New Member

    Joined:
    Sep 28, 2007
    Messages:
    535
    Likes Received:
    17
    when nothing is selected it returns nil, thats the correct behavior (you have to check if its true before trying to use it, hence you get faults)
    you were expecting a number, perhaps, in which case maybe you want to assign it to be already selected when the control is created. you could set it to be pressed already for one of the options. good luck
  6. vieestchien

    vieestchien New Member

    Joined:
    May 7, 2008
    Messages:
    43
    Likes Received:
    1
    I thought a function whose return type is int can never return nil (Java type of thinking)! I didn't check for nil, just -1. You are right, if I check to see if it is nil first, it works.

    I also thought about setting a default selected segment, but the problem is my control only has 2 segments. A new user will have a hard time to figure out which segment is actually on, which one is actually off (when you press again on a already-pressed segment, it will be turned off, unlike regular radio button group).

    Nevermind, it all works now. Thanks a bunch

    Please Register or Log in to view images

    .
  7. optimo

    optimo New Member

    Joined:
    Sep 28, 2007
    Messages:
    535
    Likes Received:
    17
    yes, I have been learning some new objC perks, the concept of `Nil` is relatively new. I got it confused with NULL, which is a different thing entirely. Think of Nil as another way of return false, but it's also used with some object management.

    glad I could help

Share This Page