Claude,The following code shows sub view counts.class MainViewController: NSViewController {
@IBOutlet weak var displayView: NSView!
override func viewWillAppear() {
super.viewWillAppear()
print("No. of subviews before the display view shows up", displayView.subviews.count) // => 0
for sub in displayView.subviews {
sub.removeFromSuperview()
}
}
override func viewDidAppear() {
super.viewDidAppear()
for sub in displayView.subviews {
sub.removeFromSuperview()
}
print("No. of subviews after the display view shows up", displayView.subviews.count) // => 0
}
override func viewWillDisappear() {
super.viewWillDisappear()
print("No. of subviews right before the display view hides itself", displayView.subviews.count) // => 3
for sub in displayView.subviews {
sub.removeFromSuperview()
}
}
override func viewDidDisappear() {
super.viewDidDisappear()
print("No. of subviews after the display view hides itself", displayView.subviews.count) // => 0
}
}Initially, the memory consumption stays around 100 MB. After I hide the window and bring it back five times or so, it will go up to around 480 MB.
Post
Replies
Boosts
Views
Activity
I don't necessarily mean to critisize how Apple, Inc. runs App Store and Mac App Store. But dealing with their reviewers is like playing the Russian roulette. Most reviewers find no problem if I give users one week of free trial and then validate their in app purchase status. But one reviewer now says no. Some reviewers seem to accept the idea that a desktop application can have a subscription-based IAP. Mine was rejected several months ago. It's very difficult for me to figure out what is acceptable and what is not.
Thanks, OOper. No, I don't insist on using tags. Could you give me some idea of how it could be done without the tag, please?
That's interesting. Thanks, OOper. I have to take a close look at what you have done.
Thank you, again, OOPer. That's an odd closure expression, which I have never seen in Swift.
Thank you, OOPer. The concern that I had with Text is that the string won't be necessarily placed inside the corresponding button. If I use your code, a small number will appear to the left of the corresponding button, not inside the corresponding button. I'm sorry if I failed to explain my needs initially.
Interestingly, there is a function called overlay.
Button("1") {
		
}
.overlay(Text("4").font(.footnote).foregroundColor(.blue).offset(x: -16, y: -16))
.buttonStyle(BorderlessButtonStyle())
.frame(width: 48, height: 48, alignment: .center)
.background(
		RoundedRectangle(cornerRadius: 2)
				.fill(Color.white)
				.shadow(color: Color.black.opacity(0.4), radius: 2, x: 0, y: 0)
)
Do I have to call it with onAppear like the following?
import SwiftUI
struct ContentView: View {
		@State private var eventPresented = false
		@State var fillColors: [Color] = Array(repeating: Color.white, count: 38)
		@State var buttonTitles: [String?] = Array(repeating: nil, count: 38)
		
		var body: some View {
				VStack {
						ZStack() {
								
						}.onAppear { makeButtons() }
						
						/* cal buttons */
						VStack(spacing: 0.0) {
								HStack(spacing: 0.0) {
										ForEach((0...6), id: \.self) {
												Button(buttonTitles[$0] ?? "") {
														eventPresented = true
												}
												.buttonStyle(BorderlessButtonStyle())
												.frame(width: 48, height: 48, alignment: .center)
												.background(RoundedRectangle(cornerRadius: 2)
																				.fill(fillColors[$0])
																				.shadow(color: Color.black.opacity(0.4), radius: 2, x: 0, y: 0)
												)
										}
								}
								...
								...
								...
								HStack(alignment: .top, spacing: 0.0) {
										ForEach((35...36), id: \.self) {
												Button(buttonTitles[$0] ?? "") {
														
												}
												.buttonStyle(BorderlessButtonStyle())
												.frame(width: 48, height: 48, alignment: .center)
												.background(RoundedRectangle(cornerRadius: 2)
																				.fill(fillColors[$0])
																				.shadow(color: Color.black.opacity(0.4), radius: 2, x: 0, y: 0)
												)
										}
								}
								.frame(width: 336.0, height: 48.0, alignment: .leading)
						}
				}.frame(minWidth: 370, idealWidth: 370, maxWidth: 370, minHeight: 420, idealHeight: 420, maxHeight: 420, alignment: .top)
		}
		
		func makeButtons() {
				buttonTitles.removeAll()
				for i in 0..<38 {
						buttonTitles.append(String(i + 1))
				}
				fillColors.removeAll()
				for _ in 0..<2 {
						fillColors.append(Color.clear)
				}
				for _ in 0..<36 {
						fillColors.append(Color.white)
				}
		}
}
What is important is that do you want to invoke makeButtons at each time when the ZStack appears? In fact, the answer is Yes, OOPer. Thanks for asking the question.
Thank you, workingdogintokyo and OOPer. This time, I choose my own answer as a solution.
Seems to be the same issue as in this thread. Yes. I thought so. I tried using it inside the button, which failed. I guess I need to go back to reading a book. I have one, but... Thanks a lot.
That unable to type-check may occur when there is some primitive errors in such declarations. OOPer, thank you. With all due respect, the code will run without the third parameter.
The variables are set up as follows.
weekIndex = 2
for _ in 0..<2 {
	 buttonTitles.append("")
	 fillColors.append(Color.clear)
	 shadowColors.append(Color.clear)
	 titleColors.append(Color.clear)
}
for i in 0..<38 - 2 {
	 if i < numberOfDays {
			buttonTitles.append(String(i + 1))
			if let day = myDay {
				 if i + 1 == day {
						fillColors.append(Color.green)
						titleColors.append(Color.white)
						} else {
						fillColors.append(Color.white)
						titleColors.append(Color.black)
				 }
				 } else {
				 fillColors.append(Color.white)
				 titleColors.append(Color.black)
			}
			shadowColors.append(Color.black.opacity(0.4))
			} else {
			buttonTitles.append("")
			fillColors.append(Color.clear)
			shadowColors.append(Color.clear)
			titleColors.append(Color.clear)
	 }
}
If I run code with just one horizontal stack
var body: some View {
	 VStack(spacing: 0.0) {
			HStack(spacing: 0.0) {
				 ForEach((0...6), id: \.self) {
						index in
						Button(buttonTitles[index] ?? "") {
							 eventPresented = true
							 selectedEventIndex = index + 1 - self.weekIndex
						}
						.foregroundColor(titleColors[index])
						.overlay(Text(eventNumbers[index] ?? "").font(.footnote).foregroundColor(.blue).offset(x: -16, y: -16))
						.buttonStyle(BorderlessButtonStyle())
						.frame(width: 48, height: 48, alignment: .center)
						.background(RoundedRectangle(cornerRadius: 2)
						.fill(fillColors[index])
						.shadow(color: shadowColors[index], radius: 2, x: 0, y: 0)
						)
						.sheet(isPresented: $eventPresented) {
							 EventView(eventVisible: self.$eventPresented, dayFromParent: self.$selectedEventIndex, monthFromParent: 11)
						}
				 }
			}
			.frame(width: 336.0, height: 48.0, alignment: .leading)
	 }
}
, the compiler won't do it.
Looks like there is quite a bit of repetitive code, I'd extract it to subviews / shared components and in general try keeping views more lightweight. ilionic, thank you.
If I run code with just one horizontal stack like
var body: some View {
	 VStack(spacing: 0.0) {
			HStack(spacing: 0.0) {
				 ForEach((0...6), id: \.self) {
						index in
						Button(buttonTitles[index] ?? "") {
							 eventPresented = true
							 selectedEventIndex = index + 1 - self.weekIndex
						}
						.foregroundColor(titleColors[index])
						.overlay(Text(eventNumbers[index] ?? "").font(.footnote).foregroundColor(.blue).offset(x: -16, y: -16))
						.buttonStyle(BorderlessButtonStyle())
						.frame(width: 48, height: 48, alignment: .center)
						.background(RoundedRectangle(cornerRadius: 2)
						.fill(fillColors[index])
						.shadow(color: shadowColors[index], radius: 2, x: 0, y: 0)
						)
						.sheet(isPresented: $eventPresented) {
							 EventView(eventVisible: self.$eventPresented, dayFromParent: self.$selectedEventIndex, monthFromParent: 11)
						}
				 }
			}
			.frame(width: 336.0, height: 48.0, alignment: .leading)
	 }
}
, the compiler won't do it. Could you suggest to me how I could improve it? Thank you.
Oops, my apology... It was just a coding error that has caused this compiler error. I guess that's what OOPer said under a similar topic. Silly me...
Oops... My mistake... I've resolved the problem.