0

I'm adding multiple buttons to the navigation bar like so.

UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];

NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2];
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(assignSelected)];
bi.style = UIBarButtonItemStyleBordered;

[bi setEnabled:FALSE];

[buttons addObject:bi];
[buttons addObject:self.editButtonItem];

[tools setItems:buttons animated:NO];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];

self.editButtonItem.title = @"Select";

Essentially I'm doing this so they can use the edit button to select a group of items and take action on them. I want to enable to action button I added in there when they are editing. I have the following method to control what the buttons say when editing.

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

    if (editing)
    {
        self.editButtonItem.title = NSLocalizedString(@"Cancel", @"Cancel");
    }
    else
    {
        self.editButtonItem.title = NSLocalizedString(@"Select", @"Select");
    }
}

That is where I want to enable and disable the action button. I'm not sure though how to access the button since it was created programmatically.

Jhorra
  • 6,233
  • 21
  • 69
  • 123

2 Answers2

1

You should be able to access it via the "bi" variable that you created it with. The bi variable should hold the pointer to the button. So you can call methods on that button like so:

[bi methodName:methodArg];

Kyle
  • 14,036
  • 11
  • 46
  • 61
  • After thinking about your question a little more, I realize you're probably having problems accessing 'bi' from a different method. Set up the UIBarButtonItem *bi in the header file and leave it equal to nil. `UIBarButtonItem *bi = nil;` then in your implementation file, leave off the class and pointer and initialize the variable from your header file. `bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(assignSelected)];` then all the methods in the class should have access to it. – Kyle Apr 25 '12 at 22:17
  • Such an obvious answer, I should have thought of that. – Jhorra Apr 25 '12 at 22:26
0

I am creating buttons dynamically using innerHTML, but cannot seem to access them on the DOM after. I try to create a node list of buttons using queryselectorall, but the list is empty. Sorry if this seems a somewhat basic question.

 const displayDrivers = (drivers) => {
  const displayDrivers = drivers
    .map((driver) => {
      return

    <div class="driver-profile">
        <div class="driver-profile__shape--id-cont">
          <img src="${
            driver.image
          }" alt="Driver Image" class="driver-card__img" />
          <p class="driver-profile__paragraph">${driver.firstName}
          &nbsp;${driver.surname}</p>
          <div class="driver-profile__rating">
            <div class="driver-profile__stars">
              <svg class="driver-card__icon-star">
                <use xlink:href="img/sprite.svg#icon-star"></use>
              </svg>
              <svg class="driver-card__icon-star">
                <use xlink:href="img/sprite.svg#icon-star"></use>
              </svg>
              <svg class="driver-card__icon-star">
                <use xlink:href="img/sprite.svg#icon-star"></use>
              </svg>
              <svg class="driver-card__icon-star">
                <use xlink:href="img/sprite.svg#icon-star"></use>
              </svg>
              <svg class="driver-card__icon-star">
                <use xlink:href="img/sprite.svg#icon-star_half"></use>
              </svg>
            </div>
            <div class="driver-profile__rating--score">${driver.calcRating()}</div>
          </div>
          <div class="driver-profile__lesson-count">${
            driver.lessonsCompleted
          } lessons</div>
          <button class="btn" type="submit" name="alter-submit">Book</button>
        </div>
        <!-- calendar and price per hour -->
        <div class="driver-profile__calendar-cont">
          <h4 class="heading--fourth is-visible">Availability</h4>
          <div class="driver-profile__calendar">
            <img
              src="../img/calendar.jpg"
              alt="Driver image"
              class="driver-profile__calendar-image"
            />
          </div>
          <div class="driver-profile__price">Lesson price: €${
            driver.prices["1 hour"]
          } p/h</div>
        </div>

        <!-- bio and qualifications -->
        <div class="driver-profile__bio">
          <div class="driver-profile__bio--btn-cont">
            <button class="square-btn videoBtn">Video</button>
            <button class="square-btn bioBtn">Biography</button>
            <button class="square-btn priceBtn">Prices</button>
            <button class="square-btn reviewBtn">Reviews</button>
          </div>
          <div class="instruction__video-wrapper is-visible">
            ${driver.videoLink}
          </div>
          <p class="driver-profile__bio-paragraph hide">
            ${driver.biography}
          </p>
          <p class="driver-profile__prices hide">
            <span class="driver-profile__span">
              1 hour lesson:&nbsp;
              <span class="driver-profile__span--2">€${
                driver.prices["1 hour"]
              }</span>
            </span>
            <span class="driver-profile__span">
              90 minute lesson:&nbsp;
              <span class="driver-profile__span--2">€${
                driver.prices["90 minutes"]
              }</span>
            </span>
            <span class="driver-profile__span">
              5 x 1 hour lessons:&nbsp;
              <span class="driver-profile__span--2">€${
                driver.prices["5 x 1 hour"]
              }</span>
            </span>
            <span class="driver-profile__span">
              10 x 1 hour lessons:&nbsp;
              <span class="driver-profile__span--2">€${
                driver.prices["10 x 1 hour"]
              }</span>
            </span>
          </p>
        </div>
      </div>

    })
    .join("");

  driversSection.innerHTML = displayDrivers;
};

window.addEventListener("DOMContentLoaded", () => {
  displayDrivers(drivers);
});

// select btns

const videoBtns = document.querySelectorAll(".videoBtn");
const bioBtns = document.querySelectorAll(".bioBtn");
const priceBtns = document.querySelectorAll(".priceBtn");
const reviewBtns = document.querySelectorAl

l(".reviewBtn");