简体   繁体   English

如何访问以编程方式创建的按钮?

[英]How do I access programmatically created buttons?

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.我不确定如何访问该按钮,因为它是以编程方式创建的。

You should be able to access it via the "bi" variable that you created it with.您应该能够通过您创建它的“bi”变量来访问它。 The bi variable should hold the pointer to the button. bi 变量应该保存指向按钮的指针。 So you can call methods on that button like so:因此,您可以像这样调用该按钮上的方法:

[bi methodName:methodArg];

I am creating buttons dynamically using innerHTML, but cannot seem to access them on the DOM after.我正在使用 innerHTML 动态创建按钮,但之后似乎无法在 DOM 上访问它们。 I try to create a node list of buttons using queryselectorall, but the list is empty.我尝试使用 queryselectorall 创建按钮的节点列表,但列表为空。 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"); l(".reviewBtn");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果是在Storyboard中创建的,如何以编程方式更改UIToolbar按钮? - How do I change UIToolbar buttons programmatically if created in Storyboard? 我以编程方式创建了矩形,并在情节提要板上添加了按钮,这些按钮正被矩形覆盖。 我该如何解决? - I programmatically created rectangles, and added buttons on storyboard and the buttons are getting covered by the rectangles. How do I fix that? 如何在另一个函数中访问以编程方式创建的子视图 - How do I access programmatically created child views in another function 如何在Xamarin中以编程方式创建不规则按钮? - How do I programmatically create irregular buttons in Xamarin? 如何以编程方式对按钮施加约束(水平间距) - How do I programmatically put constraint to buttons (horizontal spacing) 如何在Swift中以编程方式旋转一组按钮? - how do I rotate a group of buttons programmatically in Swift? 我已经以编程方式创建了一系列按钮。 单击该如何识别它们? - I've created a series of buttons programmatically. How can I recognize them when clicked? 如何在创建的数组中排列按钮? - How do I space out the buttons in an array created? 如果使用VFL以编程方式创建约束,如何为约束变化设置动画? - How do I animate constraint changes if constraints are created programmatically with VFL? 如何从以编程方式创建的UITableView中进行筛选? - How do I segue from a programmatically created UITableView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM