[Help] Slider to increase text size

Discussion in 'iOS Programming' started by joserskid, Jan 2, 2012.

  1. joserskid macrumors newbie

    joserskid

    Joined:
    Dec 30, 2011
    #1
    I have a simple app I'm trying to make, and need some assistance (I'm new :D)

    Heres what I have so far:

    ViewController.m
    Code:
    @implementation ViewController
    @synthesize textSize;
    
    -(IBAction)hello:(id)sender{
        label.text =@"Sexy Beast ;)";
    }
    -(IBAction)food:(id)sender{
        label.text =@"Bacon :P";
    }
    -(IBAction)size:(id)sender{
       // NSString *textSize = [sender value:slider];
    }
    
    ViewController.h
    Code:
    @interface ViewController : UIViewController{
        IBOutlet UILabel *label;
        IBOutlet UISlider *slider;
    }
    
    @property (nonatomic ,retain) IBOutlet UILabel *textSize;
    
    -(IBAction)hello:(id)sender;
    -(IBAction)food:(id)sender;
    -(IBAction)size:(id)sender;
    
    @end
    
    In general, I just want to get the slider value, and use that to set the size of the text label.
     
  2. xcodeNewbie macrumors member

    Joined:
    Jul 1, 2011
    #2
    Change Text Size

    You are trying to sett an NSString object equal to a slider value. That's not going to get you anything. Make sure your slider's min and max are at appropriate values, and that it's wired up to send the message "size". Then:

    Code:
    -(IBAction)size:(id)sender {
    
         float textSize = [sender floatValue]; // We get the value of the slider
    
         hello.font = [UIFont fontWithName:@"Helvetica" size:textSize]; // Change the text size of the labels
         food.font = [UIFont fontWithName:@"Helvetica" size:textSize];
    }
    You can use any font you want though.
     
  3. joserskid thread starter macrumors newbie

    joserskid

    Joined:
    Dec 30, 2011
    #3
    I'm getting a SIGABRT error now :/

    It's saying local declaration of 'text size' hides instance variable.
     
  4. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    That error means that in your .h file you've declared an instance variable (aka "ivar") with the same name as a local variable, in this case, "textSize". You should probably delete the declaration of "textSize" in your .h file (unless you need it for some other reason... in which case maybe use separate names for the instance variable and local variable.)
     
  5. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #5
    Sounds to me like your mixing things up. The "'text size' hides instance variable" would be a warning during compile time, or in the editor in Xcode 4. Due to variable scope rule, what that is saying is that your ivar version of that variable will not be used in that line.

    The SIGABRT error is something that happens during app execution. You may get a hint of where that crash is occurring. Perhaps a line number.

    P.S. Be precise with your warning and error lines. The above one is not correct. Given your posted code, I'd expect it to be "Local declaration of 'textSize' hides instance variable.". Your posted code is sparse too not to mention the ivar you mention is commented out.
     
  6. joserskid, Jan 3, 2012
    Last edited: Jan 3, 2012

    joserskid thread starter macrumors newbie

    joserskid

    Joined:
    Dec 30, 2011
    #6
    Such a rookie mistake on my part, sorry!
    Glad you understood without my posting the exact error.

    I got rid of that error by deleting the @synthesize and the declaration in the header.
    However, when I go to move the slider in the simulator, it still gives this:
    Code:
    @autoreleasepool {
            return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
        }
    
    Thread 1: Program received signal: "SIGABRT" 
    The second line of the code is what is highlights and gives the SIGABRT error. (It's in the main.m file)

    I should probably ask this. How do I link the slider to the variable textSize? Is there anything I should drag onto the view controller and connect with the storyboard? If I don't theres no error but also no text size change. If I drag from 'Value Changed' onto the view controller and connect it to 'size: ' it returns this error when I simulate and touch the slider. Sorry guys, fairly new to this :p
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    So, you've removed the textSize property from your code, right? Did you remember to remove the connection in your storyboard?

    And does your size: method still contain:
    Code:
    -(IBAction)size:(id)sender{
       // NSString *textSize = [sender value:slider];
    }
    or something else now?
     
  8. joserskid thread starter macrumors newbie

    joserskid

    Joined:
    Dec 30, 2011
    #8
    Nope I took that line out, and I took out the connection in Xcode. I'll just put what I have now.

    ViewController.h:
    Code:
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController{
        IBOutlet UILabel *label;
        IBOutlet UISlider *slider;
    }
    
    -(IBAction)hello:(id)sender;
    -(IBAction)food:(id)sender;
    -(IBAction)size:(id)sender;
    
    @end
    
    ViewController.m
    Code:
    #import "ViewController.h"
    
    @implementation ViewController
    
    -(IBAction)hello:(id)sender{
        label.text =@"Sexy Beast ;)";
    }
    -(IBAction)food:(id)sender{
        label.text =@"Bacon :P";
    }
    -(IBAction)size:(id)sender{
        float textSize = [sender floatValue];
        label.font = [UIFont fontWithName:@"Arial" size:textSize];
    }
    
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Put a breakpoint in your size: method; step through to determine which of the two lines is causing the error.
     
  10. joserskid, Jan 3, 2012
    Last edited: Jan 3, 2012

    joserskid thread starter macrumors newbie

    joserskid

    Joined:
    Dec 30, 2011
    #10
    Well now there isn't any errors, but it doesn't work at all.

    I'm just not seeing how this:
    float textSize = [sender floatValue];

    gets the value of the slider? it doesn't even make reference to the slider at all.
    I think I just don't know exactly what to connect in the story board interface.

    EDIT: I think I have everything right now :/
    it just gives that SIGABRT error every time I change the value of the slider.
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    sender is the reference to the slider. I was suggesting step-through debugging in hope you would come to this realization. But you want to cast it as such. I would do this:
    Code:
    UISlider *slider = (UISlider *)sender;
    Then you can start to use the instance methods on your slider without compiler warnings.

    Well, if you're still getting an error, I wouldn't say you have everything right. Again, step-through debugging will be the quickest way to determine where the issue is.
     
  12. joserskid, Jan 3, 2012
    Last edited by a moderator: Jan 3, 2012

    joserskid thread starter macrumors newbie

    joserskid

    Joined:
    Dec 30, 2011
    #12
    Code:
    UISlider *slider = (UISlider *)sender;
    I don't know where you'd put that.

    I have no clue how to step through debug :/
    And I get the error when I change the slider value.
    I know the error occurs here
    Code:
    label.font = [UIFont fontWithName:@"Arial" size:textSize];
    It can't change it or won't for some reason.
     
  13. joserskid thread starter macrumors newbie

    joserskid

    Joined:
    Dec 30, 2011
    #13
    Finally found the error:

    Code:
    2012-01-03 12:28:30.696 helloWorld[1949:f803] -[UISlider floatValue]: unrecognized selector sent to instance 0x6b6d0d0
    2012-01-03 12:28:30.698 helloWorld[1949:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UISlider floatValue]: unrecognized selector sent to instance 0x6b6d0d0'
    *** First throw call stack:
    (0x13b9052 0x154ad0a 0x13baced 0x131ff00 0x131fce2 0x205c 0x13baec9 0x135c2 0x1355a 0xb8b76 0xb903f 0x140467 0xb7d86 0x3893f 0x38c56 0x1f384 0x12aa9 0x12a3fa9 0x138d1c5 0x12f2022 0x12f090a 0x12efdb4 0x12efccb 0x12a2879 0x12a293e 0x10a9b 0x1ca8 0x1c05)
    terminate called throwing an exceptionCurrent language:  auto; currently objective-c
    (gdb) 
     
  14. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #14
    You'd put it in your IBAction method, right before you want to start calling instance methods on your slider.

    What kind of debugging do you know how to do? Where did you learn about it?
     
  15. joserskid thread starter macrumors newbie

    joserskid

    Joined:
    Dec 30, 2011
    #15
    I don't know any kind of debugging :p
    This is my first project in Xcode. I'm doing it to learn a little bit.
    My understanding of the code has majorly increased by doing this btw ;D
     
  16. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #16
    Maybe have a peek at this: Debugging and Analyzing Your Code

    What resources are you using to learn the fundamentals of iOS / Objective-C programming? Please be specific. If it's books, provide title, author, edition. If it's online tutorials, provide links. Etc.
     
  17. joserskid thread starter macrumors newbie

    joserskid

    Joined:
    Dec 30, 2011
    #17

Share This Page