Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8961607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:55:58+00:00 2026-06-15T15:55:58+00:00

I am building an iOS app using Rubymotion. To be able to reuse code

  • 0

I am building an iOS app using Rubymotion. To be able to reuse code I have created a file called elements.rb (a module) where I got many functions which renders different stuff. All works perfectly fine.

This is one of the functions:

def create_nav_button (hash = {})

    # Set the default values

    defaultFont = UIFont.fontWithName('Avenir-Medium', size:13)
    buttonNormal = UIImage.imageNamed('navbar/button.png')
    buttonSelected = UIImage.imageNamed('navbar/button_selected.png')

    # Create the button

    button = UIBarButtonItem.alloc.init
    button.title = hash[:title]
    button.action = hash[:action] || 'open'
    button.target = self
    button.setBackgroundImage(buttonNormal, forState:UIControlStateNormal, barMetrics:UIBarMetricsDefault)
    button.setBackgroundImage(buttonSelected, forState:UIControlStateHighlighted, barMetrics:UIBarMetricsDefault)
    button.setTitleTextAttributes({UITextAttributeFont => defaultFont}, forState:UIControlStateNormal)

    # Add button to navbar

    if (hash[:position] == 'right')

      self.navigationItem.rightBarButtonItem = button

    else

      self.navigationItem.leftBarButtonItem = button

    end

  end

This is how I call it in the controller file:

create_nav_button (:title => 'Info', :action => 'project_details', :position => 'right')

Is this a good way of reusing elements? How can I change the title of the button dynamically?

Thankful for all input!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:55:59+00:00Added an answer on June 15, 2026 at 3:55 pm

    That works. You’ll want to return the button at the end of your method:

    def create_nav_button(hash={})
      # lots of stuff
      button
    end
    

    Then call it like this:

    @right_button = create_nav_button (:title => 'Info', :action => 'project_details', :position => 'right')
    

    I don’t know of an easy way you can change the title. You could always remove the button and re-add it with a new title.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am building a iOS app using Appcelerator, I have a file app.js
I am building an iOS app using Rubymotion and when I run the project
I am building an iOS app using Rubymotion. I need to cache remote images
I am building an iOS app using Rubymotion and I need to set the
I'm using ARC in an iOS app i'm building. I've created some objects, in
I'm building an iOS app and I have all the functionality going already so
I am building an iOS, code scanner project using ZBar SDK . I am
I'm using the following code within XCode, building for iOS with ARC enabled. Why
I am building an app which uses cocos2D. I developed my app using iOS
I'm building an app using Phonegap and I'm using Urban Airship for iOS and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.