[英]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}
${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:
<span class="driver-profile__span--2">€${
driver.prices["1 hour"]
}</span>
</span>
<span class="driver-profile__span">
90 minute lesson:
<span class="driver-profile__span--2">€${
driver.prices["90 minutes"]
}</span>
</span>
<span class="driver-profile__span">
5 x 1 hour lessons:
<span class="driver-profile__span--2">€${
driver.prices["5 x 1 hour"]
}</span>
</span>
<span class="driver-profile__span">
10 x 1 hour lessons:
<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.