Cocoa:Testing for the same object with ifs/switches - objective-c

Ok so here's my code, it works great:
- (void)textViewDidChange:(UITextView *)textView{
if (textView==someObject) {
[detailItem setValue:textView.text forKey:#"someObjectAttribute"];
}
The problem is that I have lots of textviews instances to test for and I would prefer to find some other way to consolidate the code. I was thinking of something like a switch but I don't see how that would work. Any ideas?

One way would be to use the integer tag of each view. In your code, you’d have an enum like:
enum
{
kThingView,
kOtherView,
...
};
Each view's tag is set appropriately in IB or when setting up the view programatically. Then:
- (void) textViewDidChange:(UITextView *)textView
{
switch (textView.tag)
{
case kThingView:
...
}
}

Related

How to determine which ListView a delegate QML Item is in?

I have two ListViews (selection and queue) which both use the same delegate.
I want to present some of the item information differently between the two lists.
How do I determine which ListView a QML Item is in? There is no item.parent I can access in the QML.
You could use the ListView.view attached property as folibis mentioned, but there is a nicer way. If you move the delegate into its own QML file, say, Delegate.qml, you can create instances of that type that simply set a property that changes their appearances.
For example:
ListView {
// ...
delegate: Delegate {
fancy: true
}
}
ListView {
// ...
delegate: Delegate {
fancy: false
}
}
You'd code Delegate in such a way that it knows how to render a fancy/non-fancy delegate. For example:
Rectangle {
property bool fancy
color: fancy ? "steelblue" : "grey"
// ...
}

How to change PreferredStatusBarStyle programmatically

I'd like to change the color of status bar from white to black by press button, programmatically only in a single-ViewController
This is the code:
- (UIStatusBarStyle)preferredStatusBarStyle {
NSLog(#"PreferredStatusBarStyle");
if(nav_bar.alpha==1)
{
NSLog(#"->UIStatusBarStyleBlackOpaque");
return UIStatusBarStyleBlackOpaque;
}
else
{
NSLog(#"->UIStatusBarStyleLightContent");
return UIStatusBarStyleLightContent;
}}
then When I press a button action is:
[self setNeedsStatusBarAppearanceUpdate];
But this doesn't work!
When I press the button log write the correct status according to navbar.alpha, but statusbar text color remain UIStatusBarStyleBlackOpaque like when view appear.
setStatusBarStyle:animated: has been deprecated. In iOS9 you can achieve the same thing using preferredStatusBarStyle and setNeedsStatusBarAppearanceUpdate.
In your View Controller:
override func preferredStatusBarStyle() -> UIStatusBarStyle {
if (condition) {
return .LightContent
}
return .Default
}
And then when your condition changes:
self.setNeedsStatusBarAppearanceUpdate()
On Swift 4:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Hope it helps anybody else to find this post.
what you need to do, is to call the -setStatusBarStyle:animated: method thru the shared application, like this
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
you can use it without the animated parameter as well. keep in mind that UIStatusBarStyleBlackOpaque is deprecated in iOS 7, documentation says you wanna use UIStatusBarStyleLightContent instead
edit:
sorry, if u wanna use preferredStatusBarStyle, take a look at this preferredStatusBarStyle isn't called

Xcode BOOL type: from input YES, I want to get value NO

I have a very simple method, but it does not work. Ultimately, from input YES, I want to get NO and reverse.
-(void)myMethod:(BOOL)ys{
if (ys==YES) {
myLabel.hidden=NO;
myButton.hidden=NO;
}{
myLabel.hidden=YES;
myButton.hidden=YES;
}
}
Can you guys help me to correct and shorten the code? Thanks
Maybe this is a litle easier:
-(void)myMethod:(BOOL)ys{
myLabel.hidden = !ys;
myButton.hidden = !ys;
}
-(void)myMethod:(BOOL)visible
{
[myLabel setHidden:!visible];
[myButton setHidden:!visible];
}
The code should be working. However, your code is not working is probably because you did not set the referencing outlets for your label and button (If you created them with interface builder).
Referencing outlets should be set like this. Otherwise, the complier would not know if the button you want to hide is the button on the interface.
If you created the button/label with codes, check if you have them released before you are trying to set them visible or not please.
-(void)myMethod:(BOOL)ys{
if (ys) {
myLabel.hidden=NO;
myButton.hidden=NO;
} else {
myLabel.hidden=YES;
myButton.hidden=YES;
}
}
If we are trying to make it short...`
-(void)myMethod:(BOOL)ys{
myLabel.hidden = myButton.hidden = !ys;
}

How to constrain NSSplitView?

Hi I'm trying to constrain the max and min coordinate of an NSSplitView. I've created a view controller and assigned it as the delegate of an NSSplitView. The delegate methods get called however, the split view does not constrain to the position that I am trying to set it as. Any suggestions as to what I am doing wrong?
- (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMinimumPosition ofSubviewAt:(NSInteger)dividerIndex
{
NSLog(#"Constrain min");
if (proposedMinimumPosition < 75)
{
proposedMinimumPosition = 75;
}
return proposedMinimumPosition;
}
- (CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMax ofSubviewAt:(NSInteger)dividerIndex
{
NSLog(#"Constrain max");
if (proposedMax > 200)
{
proposedMax = 200;
}
return proposedMax ;
}
Let's assume you want each one of two sections on a vertical splitter to be at least 70.0 high, then what you'd do this:
- (CGFloat) splitView:(NSSplitView *)splitView
constrainMinCoordinate:(CGFloat)proposedMin
ofSubviewAt:(NSInteger)dividerIndex
{
return 70.0;
}
- (CGFloat) splitView:(NSSplitView *)splitView
constrainMaxCoordinate:(CGFloat)proposedMin
ofSubviewAt:(NSInteger)dividerIndex
{
return splitView.frame.size.height - 70.0;
}
The reason for the subtraction is to dynamically account for any resizing (with autolayout, for example) of the overall NSplitView instance. If you're working with a horizontal one, then you'd need to calculate against .width instead of .height. If you have more than 2 subviews, the idea can be extended by looking at dividerIndex and applying values as you see fit.
I solved the problem by doing this.
- (BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview
{
return NO;
}

iPhone, differentiate between multiple controls?

What is the best way when setting up a UI for the iPhone to differentiate between multiple items of the same type (i.e. 2 sliders) currently I am assigning the controls unique "tags" and then querying them in Xcode. Is this the way to go or am I missing something else?
-(IBAction)switchChanged:(UISwitch*)senderSwitch {
if([senderSwitch tag] == 1) {
NSLog(#"SwitchChanged: Engines");
}
...
gary
Hey Gary! I usually choose similar approach, though I prefer using integer constants instead of 'raw' numbers. Here's an example:
const int VIEW_TAG_A_SWITCH = 1;
const int VIEW_TAG_OTHER_SWITCH = 2;
- (IBAction)switchChanged:(UISwitch*)senderSwitch {
switch (senderSwitch.tag) {
case VIEW_TAG_A_SWITCH:
// do something
break;
case VIEW_TAG_OTHER_SWITCH:
// do something else
break;
}
}
This makes code more informational and help you to deal with situations when you need to change the UIView tag value for some reason (this way you only change it once in your nib and once in your code).
It's also very handy to use enums when you need to work with a group of controls. Here's a dummy single selection group of buttons example (something similar to what <input type="option" />):
enum GenderOptionButtons {
kMaleGenderOptionButton = 10,
kFemaleGenderOptionButton,
kUndefinedGenderOptionButton,
NUM_GENDER_OPTION_BUTTONS
}
- (IBAction)buttonDidTouchUpInside:(UIButton *)senderButton {
for (int i = kMaleGenderOptionButton; i < NUM_GENDER_OPTION_BUTTONS; i ++) {
[(UIButton *)[self.view viewWithTag:i] setSelected:NO];
}
senderButton.selected = YES;
}
If you don't own direct reference (i.e. IBOutlet) to controls, then the "tag" approach is ok.
One advantage of this approach is that you can have different kind of controls calling the same action method. One major drawback of this approach is you have to keep in sync the tags between IB and XCode.