如何从此类的枚举中访问类的属性?
但是一旦实例化多个MyClass,所有实例的_toggledOn都是相同的。
我希望有一些我不知道的干净快捷的解决方法。无论如何要先谢谢你!
@IBDesignable
class MyClass: UIView, SomeProtocols {
// MARK: - ENUM
enum ToggleState {
case on
case off
var color: UIColor {
switch self {
case .on:
return _onColor
default:
return _offColor
}
}
}
// MARK: - STATICS
private static var _onColor: UIColor = #colorliteral
private static var _offColor: UIColor = #colorliteral
// MARK: - IBSTUFF
@IBInspectable var toggledOffColor: UIColor = #colorliteral {
didSet {
MyClass._offColor = toggledOffColor
updateUI()
}
}
@IBInspectable var toggledOnColor: UIColor = #colorliteral {
didSet {
MyClass._onColor = toggledOnColor
updateUI()
}
}
@IBOutlet weak var background: UIView!
@IBAction func buttonPressed(_ sender: UIButton) {
toggleState = toggleState == .off ? .on : .off
}
// MARK: - PROPERTIES
var toggleState: ToggleState = .off {
didSet { toggle() }
}
// MARK: - METHODS
func updateUI() {
background.backgroundColor = toggleState.color
background.layer.addShadow()
}
func toggle() {
background.backgroundColor = toggleState.color
}
}
答案 0 :(得分:1)
您的 //our root app component
import { Component, NgModule, VERSION } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'my-app',
template: `
<form #form="ngForm" class="edit-path__form">
<div class="edit-path__form-container">
<div class="grey-value-bold"></div>
<div class="edit-path__form-sctypes">
<div *ngFor="let scProfile of Profiles; let rowIndex = index" class="path-template" >
<label class="grey-title" style="flex-grow: 1;margin-left: 15px;" for="checkbox_{{rowIndex}}">{{scProfile.Name}}</label>
<div class="checkbox" style="border-bottom: 1px solid;">
<input
type="checkbox"
id="checkbox_{{rowIndex}}"
checked="{{scProfile.Enabled=== 'false'? null:''}}"
name="{{scProfile.ID}}"
>
<label for="checkbox_{{rowIndex}}" style="float: left; margin-left: -20px;"></label>
</div>
</div>
</div>
</div>
</form>
`,
})
export class App {
name: string;
Profiles: any;
constructor() {
this.name = `Angular! v${VERSION.full}`;
this.Profiles = [
{Name: 'Alpha', ID: '1', Enabled: false},
{Name: 'Beta', ID: '2', Enabled: false},
{Name: 'Gamma', ID: '3', Enabled: false},
{Name: 'Delta', ID: '4', Enabled: false},
{Name: 'Zeta', ID: '5', Enabled: false},
{Name: 'Rho', ID: '6', Enabled: false}
]
}
}
@NgModule({
imports: [BrowserModule, FormsModule],
declarations: [App],
bootstrap: [App],
})
export class AppModule {}
和<TextBox x: Name = "txt_Name" Header = "Name:" Margin = "5" Text = "{Binding NamePlacta, Mode = TwoWay}" IsEnabled = "True" />
是静态的,因此对于您的类的每个实例,没有单独的_onColor
和_offColor
。\
您应将_onColor
和_offColor
设为非静态,并删除_onColor
的{{1}}属性,而应在您的 class < / em>:
_offColor
您应该输入:
,而不是color
。
ToggleState
func color(forToggleState state: ToggleState) {
switch state {
case .on:
return _onColor
case .off:
return _offColor
}
}
不应是toggleState.color
的属性,因为要进行计算,您需要来自另一个对象的信息。