Change tab on custom UIView - ios

I basically just need to change tabs when the user taps a button on my custom UIView
This is my UIView implementation
#implementation CustomMenuView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIButton *searchButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 135.0, 40.0)];
searchButton.backgroundColor = [UIColor clearColor];
[searchButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
searchButton.titleLabel.font = [UIFont systemFontOfSize:15];
[searchButton setTitle:#"Search" forState:UIControlStateNormal];
searchButton.tag = 0;
[searchButton addTarget:self action:#selector(menuItemTapped:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:searchButton];
}
return self;
}
-(void)menuItemTapped:(UIButton*)sender{
self.tabBarController.selectedIndex = sender.tag;
}
And my ViewController class:
UIView *menuView;
- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat height = [UIScreen mainScreen].bounds.size.height;
menuView = [[CustomMenuView alloc]initWithFrame:CGRectMake(0, 60, 135, height)];
[self.view addSubview:menuView];
}
This crashes because the UIView does not have a reference to the tabBarController. How do I call a method on my custom view's parent or what is the best approach to solve this?

To the best of my knowledge, you could use the delegate pattern here. So you create a protocol named CustomMenuViewDelegate and declare a weak property on CustomMenuView of this type. When the menuItemTapped: method is called you call a method on the CustomMenuViewDelegate property. You can make your ViewController conform to the delegate protocol and set is as the delegate in the viewDidLoad method.

I ended up deciding to use local notifications like this:
On my custom UIView
-(void)menuItemTapped:(UIButton*)sender{
NSDictionary* userInfo = #{#"tab": #(sender.tag)};
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:#"MenuItemSelected" object:self userInfo:userInfo];
UIViewController class
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveMenuItemSelectedNotification:)
name:#"MenuItemSelected"
object:nil];
}
-(void) receiveMenuItemSelectedNotification:(NSNotification*)notification
{
if ([notification.name isEqualToString:#"MenuItemSelected"])
{
NSDictionary* userInfo = notification.userInfo;
NSNumber* tab = (NSNumber*)userInfo[#"tab"];
NSLog (#"Successfully received test notification! %i", tab.intValue);
self.tabBarController.selectedIndex = tab.intValue;
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"MenuItemSelected" object:nil];
}

Related

becomeFirstResponder not show keyboard in ios 11

I tried to show a keyboard on after loading screen like this:
-(void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
tf.borderStyle = UITextBorderStyleRoundedRect;
tf.text = #"test";
[self.view addSubview:tf];
if([tf canBecomeFirstResponder]){
[tf becomeFirstResponder]; // surely this line is called
}
}
This code works on ios 8,9,10 but not 11. I'm not sure why the keyboard isn't show automatically on ios 11 while text field is focusing (has cursor).
And in this case, keyboard's notification isn't called:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
- (void) keyboardWillShow:(NSNotification *)note {
NSDictionary *userInfo = [note userInfo];
CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
DLog(#"Keyboard Height: %f Width: %f", kbSize.height, kbSize.width);
}
I even try this:
[tf performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0];
but still not work.
I have to click on text field to bring up the keyboard.
Is there anything update from Apple which I don't know?
Update: it looks like there is something wrong with my project or all my view controllers because I can't make the keyboard showing on all screens. But when I create new project with above code, it can work well.
Here is one of the problem:
As you can see, I have to click on the textfield to show the keyboard, and from the second time, it can work properly. And this issue only happended on ios 11 (both simulator & device)
Here is the code for search field above:
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 215, 30)];
textField.backgroundColor = [UIColor clearColor];
textField.placeholder = #"Enter text to search";
textField.borderStyle = UITextBorderStyleLine;
textField.layer.masksToBounds=YES;
textField.layer.borderColor=[[UIColor lightGrayColor]CGColor];
textField.layer.borderWidth= 1.0f;
textField.returnKeyType = UIReturnKeySearch;
textField.delegate = self;
textField.clearButtonMode = UITextFieldViewModeAlways;
[UIView transitionWithView:self.navigationController.navigationBar
duration:0.55f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.navigationItem.titleView = textField;
} completion:^(BOOL finished) {
[textField becomeFirstResponder];
}];
I wonder is there anything cause conflict keyboard?
When the TextField is not yet being drawn on the screen, then becomeFirstResponder() will not work. For example when it was hidden and never drawn. Then you need to call becomeFirstResponder() after it has been drawn. Maybe this will help:
DispatchQueue.main.async {
tf.becomeFirstResponder()
}
I have created a new project and tried your code. It works normally on both simulator and device iOS11. Keyboard is showed and notification is called. This is the code i have tried.
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UITextField* tf = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
tf.borderStyle = UITextBorderStyleRoundedRect;
tf.text = #"test";
[self.view addSubview:tf];
if ([tf canBecomeFirstResponder]) {
[tf becomeFirstResponder]; // surely this line is called
}
}
- (void)keyboardWillShow:(NSNotification*)note {
NSDictionary* userInfo = [note userInfo];
CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
NSLog(#"Keyboard Height: %f Width: %f", kbSize.height, kbSize.width);
}
#end
You should check DLog method. I think keyboard's notification is called but nothing is logged because of DLog method.
Try this one. I feel textField object is getting null in completion block and your calling becomeFirstResponder on that.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 215, 30)];
textField.backgroundColor = [UIColor clearColor];
textField.placeholder = #"Enter text to search";
textField.borderStyle = UITextBorderStyleLine;
textField.layer.masksToBounds=YES;
textField.layer.borderColor=[[UIColor lightGrayColor]CGColor];
textField.layer.borderWidth= 1.0f;
textField.returnKeyType = UIReturnKeySearch;
textField.delegate = self;
textField.clearButtonMode = UITextFieldViewModeAlways;
[UIView transitionWithView:self.navigationController.navigationBar
duration:0.55f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.navigationItem.titleView = textField;
} completion:^(BOOL finished) {
[((UITextField *)self.navigationItem.titleView) becomeFirstResponder];
}];
try this;
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[theTextField resignFirstResponder];
return NO;
}
- (void)viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification {
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
}
- (void)keyboardWillHide:(NSNotification *)notification {
}
In my case the problem was, that I had 2 UIWindow objects and the one I used to add UI elements was not set as key window, and keyboard window had lower level, so keyboard was covered by my other window. I just had to call makeKeyAndVisible() on the second window. john07's comment helped me figure this out. For further reference check: https://developer.apple.com/library/archive/qa/qa1813/_index.html
It's really weird, because this issue can be fixed after showing a UIAlertView
So when I try to add a UIAlertView with loading indicator (and auto dimiss after few seconds) like this before showing keyboard with above code, it can work well.
Don't know the root cause of my project, but it works.
And it only happens on ios 11.

iOS Methods get the call but does not change color of UIVIew

I am trying to change the color of UIView to indicate network connection. In my methods turnIndicatorViewOff and turnIndicatorViewOn, I am changing the color of indicatorView to red and green respectively. And It's not showing the color change. Those methods get the call. I tried showing an alert and it works fine too but I can't hide or change the color of indicatorView. Also it's not nil. What could be wrong?
My code:
- (void) viewDidLoad
{
indicatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 2)];
indicatorView.backgroundColor = [UIColor greenColor];
[self.view addSubview:indicatorView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(pubNubConnectionNotification:) name:#"MQTT_CONNECTION_NOTIFICATION" object:nil];
}
- (void) pubNubConnectionNotification: (NSNotification *) notification
{
if([[notification object] boolValue])
{
[self turnIndicatorViewOn];
}
else
{
[self turnIndicatorViewOff];
}
}
- (void) turnIndicatorViewOff
{
indicatorView.backgroundColor = [UIColor redColor];
}
- (void) turnIndicatorViewOn
{
indicatorView.backgroundColor = [UIColor greenColor];
}

Embedding a UITableView as a container view within a UIViewController

I would like to add a textfield and send button that sticks to the bottom of uitableview similar to a chat app. I have come across comments on embedding a UITableView as a container view within a UIViewController.
However, they seem to lack an example on how to achieve this. More specifically details including where to add a textfield/button and move textfield up when keyboard appears, etc. Thanks!
follow the steps using the storyboard
1) drag a uiviewcontroller from the object library.
2) drag and drop the textfield and button and place it at the position you want
3) drag and drop a container view.
4) delete the default uiviewcontroller comes with the container view
5) drag a uitableviewcontroller and make a segue and the segue should be embedsegue.
and for keyboard handling you can go with IQKeyboardManager library https://github.com/hackiftekhar/IQKeyboardManager
A very simple chat model code, you can take a look:
#import "ViewController.h"
#interface ViewController ()
#property UIView* containerView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_containerView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-30)];
UITextField* tfInput = [[UITextField alloc] initWithFrame:CGRectMake(0, tableView.frame.size.height, [UIScreen mainScreen].bounds.size.width-50, 30)];
tfInput.backgroundColor = [UIColor grayColor];
UIButton* btnSend = [[UIButton alloc] initWithFrame:CGRectMake(tfInput.frame.size.width, tfInput.frame.origin.y, 50, 30)];
[btnSend setTitle:#"Send" forState:UIControlStateNormal];
[btnSend setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btnSend addTarget:self action:#selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[_containerView addSubview:tableView];
[_containerView addSubview:tfInput];
[_containerView addSubview:btnSend];
[self.view addSubview:_containerView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
float keyboardHeight = [aValue CGRectValue].size.height;
//Resize the container
_containerView.frame = CGRectMake(0, - keyboardHeight, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
}
-(void)btnClicked{
[self.view endEditing:YES];
}
- (void)keyboardWillHide:(NSNotification *)notification {
_containerView.frame = [UIScreen mainScreen].bounds;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
This is a screenshot for using storyboard:

NSInvalidArgumentException Custom Button with NSNotificationCenter

I am trying to create a custom button and add its class as an observer to NSNotificationCenter in the custom initializer like so.
#implementation SACenterDiskButton
-(id)initWithImage:(UIImage *)image forView:(UIView *)view;
{
self = [super init];
if (self) {
self = [UIButton buttonWithType:UIButtonTypeCustom];
self.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
double buttonWidth = view.frame.size.width/5;
self.frame = CGRectMake(0.0, 0.0, buttonWidth, 57);
self.adjustsImageWhenHighlighted = NO;
self.imageView.clipsToBounds = NO;
self.imageView.contentMode = UIViewContentModeCenter;
[self setImage:image forState:UIControlStateNormal];
[self setBackgroundColor:[UIColor colorWithRed:0.317 green:0.4 blue:0.544 alpha:1]];
[view addSubview:self];
//Add observers for when tracks are played/paused/ended for spinning the center button
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(trackPlayedNotification:) name:#"trackPlayed" object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(playPauseNotification:) name:#"togglePlayPause" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(itemDidFinishPlaying:) name:#"songEnded" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
return self;
}
#pragma mark - Notificaton Center
-(void)applicationWillEnterForeground:(NSNotification *)notification
{
BOOL playerIsPlaying = [PlayerManager sharedManager].playerIsPlaying;
if (playerIsPlaying) {
[self.imageView rotate360WithDuration];
}
}
-(void)itemDidFinishPlaying:(NSNotification *)notification
{
[self.imageView stopAllAnimations];
}
-(void)trackPlayedNotification:(NSNotification *)notification
{
[self.imageView rotate360WithDuration];
}
-(void)playPauseNotification:(NSNotification *)notification
{
NSNumber *playerIsPlaying = notification.userInfo[#"playerIsPlaying"];
if ([playerIsPlaying boolValue]) {
[self.imageView pauseAnimations];
}
else {
[self.imageView resumeAnimations];
}
}
The button is working well except for when a notification is sent to it, I get this error.
-[UIButton applicationWillEnterForeground:]: unrecognized selector sent to instance 0x17dd8930
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton trackPlayedNotificaion:]: unrecognized selector sent to instance 0x17dd8930'
*** First throw call stack:
Am I adding observers to my class properly? Any ideas why I am getting this crash?
The problem is the following line:
self = [UIButton buttonWithType:UIButtonTypeCustom];
This reassigns self to a new UIButton instance, replacing the desired new instance of your custom button class.
Simply remove that line and your code will work as expected.

NSNotificationCenter doesn't work

I do notifications like this: I have code in mainView:
- (void)viewDidLoad
{
CGRect view1Hrame;
view1Hrame.origin.x = 160;
view1Hrame.origin.y = 215;
view1Hrame.size = self.currenWeatherView.frame.size;
weatherViewController *view1 = [[weatherViewController alloc] initWithForecast:[[Forecast alloc] initWithForecastInCurrentLocation]];
[[NSNotificationCenter defaultCenter] addObserver:view1 selector:#selector(forecastChanged) name:#"forecastChanged" object:nil];
view1.view.backgroundColor = [UIColor colorWithRed:0 green:255 blue:0 alpha:0.06];
[currenWeatherView addSubview: view1.view];
}
in Forecast class:
[[NSNotificationCenter defaultCenter] postNotificationName:#"forecastChanged" object:nil];
in weatherViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(forecastChanged) name:#"forecastChanged" object:nil];
// Do any additional setup after loading the view from its nib.
}
It does not work. What did I do wrong?
if you want to call notification in weatherViewController then you need to define this class's viewDidLoad method.
Make sure you are calling viewDidLoad of this viewController.

Resources