NSStatusItem dissapears instantly after launching application [duplicate] - objective-c

This question already has answers here:
NSStatusItem appears briefly on launch, but promptly disappears
(2 answers)
NSStatusBar + Swift: title shows and immediately disappear
(1 answer)
Closed last year.
I'm working with storyboard and this is the code I have below.
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
NSStatusItem *status_item = [[NSStatusBar systemStatusBar] statusItemWithLength: -1];
[status_item setMenu:_menu_bar_item];
status_item.button.image = [NSImage imageNamed:#"MenuBarIcon"];
status_item.button.imagePosition = NSImageLeft;
status_item.button.title = #"foo";
status_item.button.toolTip = #"bar";
[status_item setVisible:true];
}
The status item does show in the menu bar for like a frame and then instantly dissapears. I've tried it with and without the image and I can't get it to show longer than a split second before it just vanishes. Does anyone know what causes this and how to prevent it?

You are creating the object and then you throw it away.
You need a strong reference for example a property
#property (strong) NSStatusItem *status_item;
and then
status_item = [[NSStatusBar systemStatusBar] statusItemWithLength: -1];

Related

iOS UIActionSheet for NSDataDetector and double modal view

I am having an issue with the detected numbers in iOS 7 and iPhone, when the user long tap the number detected by the OS, it prompts an UIActionSheet with the options: "Call", "Send Message", "Add to Contacts", "Copy" and "Cancel". The problem I am facing is, when the option "Send Message" or "Add to Contacts" is tapped, the OS creates an modal view on top of my current modal view, which leads to having the navigation bar of the second modal view not being displayed correctly.
With that in mind, I am not able to assert at which moment the user has tapped which button, because it is not me who created the UIActionSheet (iOS does itself), then I can not receive any kind of delegate methods. The only message sent to the UIViewController is:
-(BOOL)textView:(UITextView *)textView
shouldInteractWithURL:(NSURL *)URL
inRange:(NSRange)characterRange
Which tells me what kind of data was tapped once by the user (but not long tapped). I tried as well with the method call:
-(void)viewWillDisappear:(BOOL)animated
-(void)viewDidDisappear:(BOOL)animated
Unfortunately, they are never invoked on iOS7, whereas iOS8 does. Which drives me to the conclusion that, so far, this issue is only iOS7 related, I am using an iPhone 4 with iOS 7.1.2. When I tried the same case in iOS8, the second modal view renders correctly, being placed on top of my current view.
I hope someone has more info or other ideas.
Thanks!!
The solution that I ended up applying was to completely avoid the long press gesture on the title number that was generating the action sheet to appear. I did it checking on the gesture recognizer list for the UITextView that was inside the cell, then look up for one that was of the class UILongPressGestureRecognizer, inside I would just disable the friend gesture. Snippet of code is:
RestrictedTextView.h
#import <UIKit/UIKit.h>
#interface RestrictedTextView : UITextView
#end
RestrictedTextView.m
#import "RestrictedTextView.h"
NSString *const kFriendsStringInGesture = #"friends";
#implementation RestrictedTextView
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([[gestureRecognizer class] isEqual:[UILongPressGestureRecognizer class]])
{
UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)gestureRecognizer;
longPress.enabled = YES;
if ([longPress valueForKey:kFriendsStringInGesture] != nil)
{
UILongPressGestureRecognizer *friendLongPress = (UILongPressGestureRecognizer *)[[longPress valueForKey:#"friends"] anyObject];
friendLongPress.enabled = NO;
}
}
return YES;
}
#end

Why NSStatusBar's text appears and disappears instantly at app start in Yosemite 10.10?

Here is a code how I add a status bar with text:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSStatusBar *bar = [NSStatusBar systemStatusBar];
_statusBarItem = [bar statusItemWithLength:NSVariableStatusItemLength];
[_statusBarItem setTitle:#"Status"];
[_statusBarItem setHighlightMode:YES];
[_statusBarItem setMenu:_menu];
}
It appears and disappears immediately after I launch my app.
How to fix that?
I use Xcode 6.0 and Yosemite 10.10
I used an example of creating status bar from:
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/StatusBar/Tasks/creatingitems.html
Solved the problem by changing property from weak to strong for _statusBarItem

How to assign a name to every view in a storyboard

So I have an app where I use for the UI a storyboard with different views.
In the first view, there's an if statment when I push a button. It decides if the screen has to show the next ViewController or other. I already know how to do this with .xibs, but no with storyboards.
Here's the code that doesn't work:
.h
{
IBOutlet UIView *one;
IBOutlet UIView *two;
}
.m
-(IBAction)decideNextView:(id)sender{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *NextView =[defaults objectForKey:#"TestDone"];
if ([NextView isEqualToString:#""]) {
self.view = one;
}else if ([NextView isEqualToString:#"Done"]) {
self.view = two;
}else {
self.view = one;
}
}
When I run the app in my iPod Touch and I push the button that performs the IBAction, I get a black screen, but any error. Please help me!
The way you are supposed to access view elements in code is with the "Tag" item in the attributes inspector. The best way to do this is to set it to a number and then do #define kView1 0 so in code you don't need to remember what number you assigned to view1, you just use the constant.
Where it says Tag here, you can set that to any number for each item in your storyboard or xib. Then in code you can say something like:
#define NAME_TAG 0
UIView *nameView = [self.view viewWithTag:NAME_TAG];

UITabbarController default tab highlighted

So I'm attempting to deal with uitabbarcontroller and wanted to have a default tab be selected when the view loads.
I have tried setting the
#property (nonatomic) int selectedIndex;
however this just sets which viewcontroller is being shown, not the actual tabbar being selected
I also tried this line
[self tabBar].selectedItem = [[[self tabBar] items] objectAtIndex:0];
but it crashes as you are not allowed to mutate the tabbar of a tabbarcontroller
Any help with this?
Thanks
So, as discussed in the comments, following code should do what you want:
[self.tabBarController setSelectedIndex:desiredIndex];
Alternatively, you can use:
[self.tabBarController setSelectedViewController:desiredViewController];

Getting value of a button label from a different ViewController

This seems to be quite trivial but somehow I can't get it to work. I have 2 Popover Controllers with a tableView as contents, and I have a UIViewController with some buttons.
I'm setting one of the button's title from the PopoverController's didSelectRoxAtIndexPath function. This works nicely. Now I would like to pass this button title to the second PopoverController but can't seem to get this to work; the string returns a NULL. Currently I'm using:
h file:
NSString *myString;
#property(nonatomic,retain)NSString *myString;
m file:
#synthesize myString;
in viewDidLoad:
MyAppViewController *appV = [[MyAppViewController alloc]initWithNibName:#"MyAppViewController" bundle:nil];
self.myString = appV.buttonOne.currenTitle;
Try setting setting this property before displaying the second UIPopoverController.