Do you always need to initialize in basic4android? - basic4android

Do I need to initialize everything before I use it in basic4android?
Dim lbl as label
Dim lv1 as listview
lbl.initialize()
lv1.initialize()
lbl1.text = "Hello world!"

Only if you are creating the layout programmatically. If you create it with the designer you do not need to initialize the views.
Another case where you do not need to initialize is when you reference an already initialized view:
Dim lbl1, lbl2 As Label
lbl1.Initialize("")
lbl2 = lbl1
lbl2.Text = "efwef"

Related

UICollectionViewListCell Accessory TopTrailing Placement

iOS 14 introduced a bunch of advancements for UICollectionView including the new UICollectionViewListCell. Using the defaultContentConfiguration, you can add accessory views to the cell. I'm looking to recreate an iMessage conversation row (mail is also close) where the date label is in the top trailing corner. Is there anyway to do this using the default configuration? Having to create a custom cell seems like overkill just for this.
Here is what I currently have.
let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, Int> { cell, indexPath, item in
var content = cell.defaultContentConfiguration()
content.text = "Title"
content.secondaryText = "This is the body of the message and it's really long. I want to see where it finally truncates because thats what it should do eventually you know?"
content.secondaryTextProperties.numberOfLines = 2
content.image = UIImage(systemName: "star")
let label = UILabel()
label.text = "4/26/7"
label.textColor = .secondaryLabel
label.font = .preferredFont(forTextStyle: .caption1)
let customAccessory = UICellAccessory.CustomViewConfiguration(
customView: label,
placement: .trailing(displayed: .always))
cell.accessories = [.customView(configuration: customAccessory)]
cell.contentConfiguration = content
cell.tintColor = .tertiaryLabel
}
Here is my desired result
Here is mail for another example
The default UICollectionViewListCell has limited customization possibilities.
Your best shot is with a custom UICollectionViewCell subclass. You will instead have the cell registration register your custom class, and dequeue cells the same way you would dequeue a built-in UICollectionViewListCell class object.

Thread 1: signal SIGABRT swift

I was trying to center the text in the middle of a view, but instead, received an error. Here's my code as a reference.
let textbox = UITextField()
textbox.text = "Hello"
textbox.sizeToFit()
textbox.centerXAnchor.constraint(equalTo: textboxView.centerXAnchor).isActive = true
textbox.centerYAnchor.constraint(equalTo: textboxView.centerYAnchor).isActive = true
textbox.delegate = self
self.textboxView.addSubview(textbox)
You cannot constraint a view relative to another view unless they are first related.
You need to add the subview first before setting the constraints
let textbox = UITextField()
textbox.text = "Hello"
textbox.translatesAutoresizingMaskIntoConstraints = false
self.textboxView.addSubview(textbox)
textbox.centerXAnchor.constraint(equalTo: textboxView.centerXAnchor).isActive = true
textbox.centerYAnchor.constraint(equalTo: textboxView.centerYAnchor).isActive = true
textbox.delegate = self
You haven’t given lot of detail surrounding the rest of your view controller, so you may have already added it somewhere else.
However, when using autolayout constraints you must turn off auto masking.
textbox.translatesAutoresizingMaskIntoConstraints = false
You should also add the textbox as a subview prior to setting constraints.

Variable name in a loop using an integer

I want to change the text of a lot of labels. All my labels are named LabelName1, LabelName2, LabelName3.
How can I edit my labels with one loop like this:
for i in 1..10 {
LabelName(i).Text = String(some text, the some for each label)
}
You should store all of your UILabels in an array and change the text for all of them like this:
for label in labelArray {
label.text = String(some text, the some for each label)
}
Alternatively to #toddg fine answer, you could assign a specific tag to the labels and loop through the subviews:
// loop through subviews and update all UILabels with a tag property of 4
for view in self.subviews as [UIView] {
if let label = view as? UILabel {
if label.tag == 4 {
label.text = String(some text, the some for each label)
}
}
}
The advantages to this is no arrays are needed, and you can be as specific or generic as desired.

create a UIButton for each object in array

Hello guys I want to create a UIButton programmatically from every object in an array. something like
for answer in answers {
//create button
}
But how do I assign all these buttons a randomized and unique variable?
like var randomvar = UIButton() ?
I had this problem when I was new to OOP x).
Basically a variable when handling an object is just a pointer to the address in memory.
An array with objects will actually be an array with pointers to all unique objects, in your case these objects are UIButton's.
So in your for-loop you have to add those buttons inside another array.
var buttons = [UIButton]()
for answer in answers {
let btn = UIButton()
...
buttons.append(btn)
}
Then you can access each unique button by accessing them trough buttons.
buttons[2].setTitle("test 2", forControlEvent: .Normal)
Loop over your array and create the button for each item. Either add it to your view directly or save it into an array for later access
var buttons = [UIButton]()
for answers in answer {
let button = UIButton(...)
button.addTarget(self, action: "buttonAction:", forControlEvents: . TouchUpInside)
// add to a view
button.frame = ...
view.addSubview(button)
// or save for later use
buttons.append(button)
}
The better approach is to create an array property:
var array: [UIButton]?
and inside the loop create a button and add it to this array.
If you need to access any button you can access it by index.

MvvmCross: Programmatically adding a view to CollectionView's subview

Depending on user action, I want to dynamically add a view (within MvxViewController) to the subviews collection of a CollectionView. Here's what I'm doing:
Does NOT work: SubviewThree inherits MvxViewController
var mvxViewController = new SubviewThree(); // SubviewThree inherits MvxViewController
var uiView = mvxViewController.View;
uiView.ClipsToBounds = true;
uiView.Hidden = false;
uiView.Frame = new RectangleF(0, 0, 320, 200);
CollectionView.AddSubview(uiView);
CollectionView.BringSubviewToFront(uiView);
Works: SubviewThree inherits UIViewController
var mvxViewController = new SubviewThree(); // SubviewThree inherits UIViewController
var uiView = mvxViewController.View;
uiView.ClipsToBounds = true;
uiView.Hidden = false;
uiView.Frame = new RectangleF(0, 0, 320, 200);
CollectionView.AddSubview(uiView);
CollectionView.BringSubviewToFront(uiView);
Works:
var label = new UILabel(); // All simple controls work just fine
label.Text = "Test Label";
label.ClipsToBounds = true;
label.Hidden = false;
label.Frame = new RectangleF(0, 0, 320, 200);
CollectionView.AddSubview(label);
CollectionView.BringSubviewToFront(label);
What's the best way to achieve this in MvvmCross? Note that I cannot use data binding (and add it via view model's ObservableCollection) here since the view to be added depends on user action - it may not be of same type.
Here's what I'm trying to do:
On right/left button touch, I want to swap views. The views are bound to different view models. The swapped view can either be either of V1 type (having a bar and another UIView) or some other type (just a UIView).
Here's my github link with sample project, just in case if the above image is not clear. You can just run the app and click on Add button. The code for adding view to subview is in BarCell.cs, line 158. There I've commented the above mentioned lines of code.
Github Link
In order for MvvmCross to create a View and to hook it up with it's ViewModel, MvvmCross uses a MvxViewModelRequest object. In iOS this is connected to the View using a Request property.
You can hook this up yourself if you want to - it's just a get/set property that has to be set before ViewDidLoad occurs - see IMvxTouchView.cs
Alternatively:
if you want MvvmCross to create the View (including doing the ViewModel-View lookup) then there are some extension methods in https://github.com/slodge/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.Touch/Views/MvxCanCreateTouchViewExtensionMethods.cs
if you don't need a viewController, then the MvxView class may be sufficient for you - see the N=32 video for an example of its use - http://slodge.blogspot.co.uk/2013/06/n32-truth-about-viewmodels-starring.html