Something simple

Discussion in 'iOS Programming' started by GmaN98, Sep 13, 2011.

  1. GmaN98 macrumors newbie

    Sep 13, 2011
    I am not sure if this is the right place to put this but I need someone to make a tutorial or at least help me with something so simple I am embarrassed to say this.

    All I need to know is how to make multiple buttons on a app open safari and go to different URLs.

    example: youtube button goes to youtube, google goes to google.
    I have the button and the code done for the first one, and when I try a few things that a friend told me all I get are errors. most of which are him telling me to copy and paste (i think hes not so good at this)

    I will admit I am still a beginner at this, but I am always willing to learn and take advice from those who do know.

    Thank you
  2. (marc) macrumors 6502a


    Sep 15, 2010
    the woods
  3. GmaN98 thread starter macrumors newbie

    Sep 13, 2011
    Well here is what I am talking about.

    On my App I have one button that says videos, which its purpose is to open safari and take you to a specific youtube account which is set up with this section of code here:

    @implementation MezkaiViewController
    -(IBAction)openWebsite  {
    	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@""]];

    The problem is when I want to make another button say called "News" and have that certain button take you to a different specific site, there is no way to duplicate that code separate from the first one that I can find, or do you need to have separate ViewController files per button?
  4. ianray macrumors 6502

    Jun 22, 2010
    What did you try?

    Are you using Interface Builder?

    Are you familiar with the concepts of Outlets and Actions?
  5. GmaN98 thread starter macrumors newbie

    Sep 13, 2011
    Well yeah I am using Xcode 4.2, and yes I am familiar with Outlets and Actions (Well keep in mind I am learning as I go, reading everything I can get my hands on.)
  6. ArtOfWarfare macrumors G3


    Nov 26, 2007
    You want the second button to run through different code, right?

    So the simplest way is going to be just making another IBAction method with a different name.

    I'd suggest renaming the one you already have to "openYoutube" (because the way it's coded right now, it's not actually capable of doing anything else,) duplicating it, and making the other one named "openGoogle" that has the code to go to Google instead, and is hooked up to a different button which is for opening Google.

    Of course, you could have a single method named "openWebsite:", note the colin, those are important as they signal that the method accepts an argument, in this case a pointer to the UIButton invoking the method, which means you can determine which button was pressed inside the method and respond different based on that.
  7. GmaN98 thread starter macrumors newbie

    Sep 13, 2011
    Thank you for the tips ArtOfWarfare it helped me out a lot, and things are starting to work as intended without any issues, or problems at all.
  8. jnoxx macrumors 65816


    Dec 29, 2010
    Aartselaar // Antwerp // Belgium
    I'm sorry to barge in like this. I support noobies in any ways, I have used alot of resources and help on forums like this. But, basic IBActions, I think you should take a look at basic iPhone programming (gazillion books existing about this).
    Pickup a book, read it through, should only take you few hours for basics. And then I think your programming path in iOS will broaden up alot.
  9. GmaN98 thread starter macrumors newbie

    Sep 13, 2011
    jnoxx I totally understand where you are coming from, but for me I am a hands on learning type of guy so I work on this while I am reading all the stuff about iOS as I go along. Plus I find when other people who have been where I am explain something in a way I can understand it a little better, it helps more than a book that explains it only in the way it was written. I am slowly taking my time to get better while at the same time I am not running through it just to act like I am some overnight master programmer. I would rather come to others for help than to try to build something thinking I know more than I actually do. Sorry for the wall of text, kind of felt i needed to explain myself for when i might need help again.
  10. jnoxx macrumors 65816


    Dec 29, 2010
    Aartselaar // Antwerp // Belgium
    I have been in the same position, the problem is, you just don't get as far as you think you would go. I allways thought like, oh, i'm understanding alot now! (after i checked/copied/typed my way through basic apps).
    But then the real stuff starts (i'm full time iOS Developer), and every time you get a new challenge, you have the feeling the knowledge is lacking.
    Without offending, I have been in the same position, so with that in mind, I just want to assure "new people", that reading a book alongside doing what you do is actually alot better. Well, depends on the book.. If you for example take the cooking book of Erica Sudsen or something, that's the first one i had my hands on. She just explains stuff in easy ways, which you can "easily" implement in your own little projects. Which will set you way further, then wasting time on google to do basic stuff actually.
    All & all, have fun. that's the main deal.

Share This Page