In my viewcontroller like this:
#import "PPSharedView.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
PPSharedView * sharedView = [[PPSharedView alloc] init];
sharedView.topImageName = #"fenxiangdao";
[sharedView sharedWithArrayImage:#[#"weixin_um",#"weixin_um",#"weixin_um",] titles:#[#"微信好友",#"微信好友",#"微信好友",]];
}
#end
In PPSharedView.h like this:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#interface PPSharedView : UIView
-(void)sharedWithArrayImage:(nonnull NSArray *)arrImgs
titles:(nonnull NSArray *)arrTitles;
#end
In PPSharedView.m on the below:
-(void)sharedWithArrayImage:(NSArray *)arrImgs titles:(NSArray *)arrTitles{
if (arrImgs.count != arrTitles.count) return;
UIWindow * keyWindow = [UIApplication sharedApplication].keyWindow;
UIView * backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_W, kSCREEN_H)];
[keyWindow addSubview:backView];
backView.backgroundColor = [UIColor blackColor];
backView.alpha = 0.5;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(clickRedview)];
backView.userInteractionEnabled = YES;
[backView addGestureRecognizer:tap];
}
-(void)clickRedview{
NSLog(#"adf");
}
When I run the app on the iPhone the backView on the screen but can't response click events.Anyone knows what's wrong with the code?Thank you very much!
In controller the sharedView should be referenced example:
#interface ViewController ()
#property(nonatomic, strong) PPSharedView * ppView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
PPSharedView * sharedView = [[PPSharedView alloc] init];
self.ppView = sharedView;
sharedView.topImageName = #"fenxiangdao";
[sharedView sharedWithArrayImage:#[#"weixin_um",#"weixin_um",#"weixin_um",] titles:#[#"微信好友",#"微信好友",#"微信好友",]];
}
#end
If don't do this the sharedView will be destoryed!
Related
I created a NavigationBar and added it to the UIViewController. But after init, the reference turns to nil. I'm new to iOS and OC, I don't know why. Anyone can help? Thank you.
code summary:
#interface ContainerViewController()
#property (nonatomic, retain) UINavigationBar *nav;
#property (nonatomic, retain) UINavigationItem *navItem;
#end
#implementation ContainerViewController
- (instancetype) initWithParams:(NSDictionary *)params {
self = [super init];
if (self) {//...}
return self;
}
- setNavTitle:(NSDictionary *) params {
NSString *title = params[#"title"];
/////////////////////////////////
// here goes wrong
// self.navItem == nil here, why?
/////////////////////////////////
self.navItem.title = title;
}
- (void) viewWillAppear:(Bool)animated {
[super viewWillAppear:NO];
static float navHeight = 64.0;
UIViewController *wvController = [WebView init here];
UINavigationBar *nav = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), navHeight)];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:title];
nav.items = [NSArray arrayWithObjects: navItem, nil];
///////////////////////////////
// I saved the reference here
//////////////////////////////
[self setNav:nav];
[self setNavItem:navItem];
[self.view addSubview:nav];
[self addChildViewController:wvController];
wvController.view.bounds = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - navHeight);
[self.view addSubview:wvController.view];
[wvController didMoveToParentViewController:self];
}
#end
This will be useful for you, kindly check and do
Tutorial point site is very easy to learn some important UI basics if you are working in Objective C
i want to do like on this picture:
i do templateDisplayNode:
#interface TemplateDisplayNode : ASDisplayNode
#property (nonatomic, strong) ASDisplayNode *headerNode;
#property (nonatomic, strong) ASDisplayNode *ContentNode;
- (void)addContentNode:(ASDisplayNode *)contentNode;
#end
#implement TemplateDisplayNode
- (instancetype)init
{
self = [super init];
if (self) {
[self createSubviews];
}
return self;
}
- (void)createSubviews {
[self createHeaderNode];
[self createContentNode];
}
- (void)addContentNode:(ASDisplayNode *)contentNode {
[self.contentNode addSubnode:contentNode];
}
- (void)createHeaderNode {
self.headerNode = [[ASDisplayNode alloc] init];
self.headerNode.backgroundColor = [UIColor blueColor];
[self addSubnode:self.headerNode];
}
- (void)createContentNode {
self.contentNode = [[ASDisplayNode alloc] init];
self.contentNode.style.flexGrow = YES;
[self addSubnode:self.contentNode];
}
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize {
CGFloat mainScreenWidth = [UIScreen mainScreen].bounds.size.width;
NSMutableArray *mainStackContent = [[NSMutableArray alloc] init];
if (self.headerNode) {
self.headerNode.style.preferredSize = CGSizeMake(mainScreenWidth, 48);
[mainStackContent addObject:self.headerNode];
}
if (self.contentNode) {
[mainStackContent addObject:self.contentNode];
}
ASStackLayoutSpec *contentSpec =
[ASStackLayoutSpec
stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical
spacing:0
justifyContent:ASStackLayoutJustifyContentStart
alignItems:ASStackLayoutAlignItemsStretch
children:mainStackContent];
return contentSpec;
}
#end
and create page of TemplateDisplayNode class:
#interface Page1Node : TemplateDisplayNode
#end
#implement Page1Node
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize {
[super layoutSpecThatFits:constrainedSize];
}
#end
And use it in ASViewController:
#interface MyViewController : ASViewController
#property (nonatomic, strong) MyCustomASViewControllerWithTableNode *myTableNodeViewController;
#property (nonatomic, strong) Page1Node *page1node;
#end
#implement MyViewController
- (instancetype)init {
self.page1node = [[Page1Node alloc] init];
self = [super initWithNode:self.page1node];
if (self) {
[self createMyTableNodeViewController];
}
return self;
}
- (void)createMyTableNodeViewController {
self.myTableNodeViewController = [[MyCustomASViewControllerWithTableNode alloc] init];
[self.page1node addContentNode:self.myTableNodeViewController.node]; //add TableNode like content on page1node
[self addChildViewController:self.myTableNodeViewController];
[self.myTableNodeViewController didMoveToParentViewController:self];
}
#end
MyCustomASViewControllerWithTableNode:
#interface MyCustomASViewControllerWithTableNode : ASViewController
#property (nonatomic, strong) ASTableNode *tableNode;
#end
...
- (instancetype)init {
_tableNode = [[ASTableNode alloc] initWithStyle:UITableViewStylePlain];
self = [super initWithNode:_tableNode];
if (self) {
_tableNode.dataSource = self;
_tableNode.delegate = self;
}
return self;
}
and when i run it, I see a table in the content field, but I can not scroll the table, because Size frame ContentNode = {0; 0};
How to make layout correctly in order to set the size? If I do the same thing, but without using TemplateDisplayNode, then everything works. I would like to use TemplateDisplayNode to remove a lot of duplicate code for similar pages.
AsyncDisplayKit documentation is not advised to use:
[super layoutSpecThatFits:constrainedSize];
If I remove this line, then I do not go into layoutSpecThatFits in TemplateDisplayNode.
Advise how to be? What solutions can be?
I have tried modify my code many times, but still can't pass variable from UIViewController to UIView, variable always return (null).
// BPGraphView.h
#interface BPGraphView : UIView
#property (nonatomic, retain) NSString *test;
#end
// BPGraphView.m
#import "BPGraphView.h"
#implementation BPGraphView
#synthesize test;
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
NSLog(#"test %#",test);
if (self) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect
{
NSLog(#"draw %#", test); // always return (null)
if ([test isEqual:#"something"]) {
[self drawOutLine];
}
}
#end
// BloodPressureViewController.m
- (void)viewDidLoad
{
BPGraphView * graphview=[[BPGraphView alloc] init];
graphview.test = #"something";
}
In your code:
- (void)viewDidLoad
{
BPGraphView * graphview=[[BPGraphView alloc] init];
graphview.test = #"dd";
}
The variable graphview is basically destroyed once the viewDidLoad is run to finish. It will never get a chance to run drawRect.
Now the question is how should you define a instance variable of BPGraphView in your UIViewController. The easiest way should be adding a BPGraphView onto your view's xib file and link to an IBOutlet in your UIViewController. In this way, you should be able to assign to test
#IBOutlet BPGraphView graphview;
- (void)viewDidLoad
{
graphview.test = #"dd";
}
Without setting a frame, drawRect will not be called.
BPGraphView * graphview=[[BPGraphView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
graphview.test = #"something";
[self.view addSubview:graphview];
I tried this,it works well,in your code,drawRect is not called
BPGraphView * graphview=[[BPGraphView alloc] init];
graphview.test = #"dd";
[graphview drawRect:CGRectMake(0, 0, 0, 0)];
This is firstPopoverViewController.h code:
#import <UIKit/UIKit.h>
#interface firstPopoverViewController : UIViewController
#end
This is my firstPopoverViewController.m code:
#import "firstPopoverViewController.h"
#interface firstPopoverViewController ()
#end
#implementation firstPopoverViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.contentSizeForViewInPopover = CGSizeMake(300, 290);
// Header label
UILabel *h1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 85)];
h1.font = [UIFont fontWithName:#"myFont" size:22.0];
h1.textColor = [UIColor blackColor];
h1.textAlignment = NSTextAlignmentCenter;
h1.text = #"Heading";
h1.numberOfLines = 0;
h1.backgroundColor = [UIColor greenColor];
// Ok button BG View
UIView *buttonBG = [[UIView alloc] initWithFrame:CGRectMake(0, 300-75, 300, 75)];
buttonBG.backgroundColor = [UIColor greenColor];
// Ok button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(300/2-130/2, 290-35-15, 130, 35);
button.backgroundColor = [UIColor whiteColor];
[button setTitle:#"OK" forState:UIControlStateNormal];
[button addTarget:self action:#selector(closePop) forControlEvents:UIControlEventTouchUpInside];
button.adjustsImageWhenHighlighted=YES;
// Adding views
[self.view addSubview:h1];
[self.view addSubview:buttonBG];
[self.view addSubview:button];
}
-(void)closePop {
}
#end
Then there's ViewController.h:
#import <UIKit/UIKit.h>
#import "firstPopoverViewController.h"
#interface ViewController : UIViewController
#property (strong, nonatomic) UIButton *popButton;
#property (strong, nonatomic) firstPopoverViewController *firstPopoverViewController;
#property (strong, nonatomic) UIPopoverController *firstPopover;
#end
And finally ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
/* ##### UIPopController stuff ##### */
UIImage *popButtonImage = [UIImage imageNamed:#"menu.png"];
_popButton = [UIButton buttonWithType:UIButtonTypeCustom];
_popButton.frame = CGRectMake(0, 0, 73, 66);
[_popButton addTarget:self action:#selector(openPop) forControlEvents:UIControlEventTouchUpInside];
_popButton.adjustsImageWhenHighlighted=NO;
[_popButton setImage:popButtonImage forState:UIControlStateNormal];
[self.view addSubview:_popButton];
}
-(void)openPop {
if (_firstPopoverViewController == nil) {
//Create the _firstPopoverViewController.
_firstPopoverViewController = [[firstPopoverViewController alloc] init];
}
if (_firstPopover == nil) {
_firstPopover = [[UIPopoverController alloc] initWithContentViewController:_firstPopoverViewController];
_firstPopover.popoverContentSize = CGSizeMake(300, 290);
[_firstPopover presentPopoverFromRect:CGRectMake(0,0, 73, 66) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
NSLog(#"show");
} else {
NSLog(#"dismiss");
[_firstPopover dismissPopoverAnimated:YES];
_firstPopover = nil;
}
}
#end
It's pretty basic code that displays a button and when I click this button it shows popover. I want to close this popover using button that's inside firstPopoverViewControll.m file. There's a closePop{} method, what should I put inside it to close this popover? Thanks.
By the way I'm beginner as you can see, I researched stackoverflow and there are some solutions with delegates, which seems to be working for others, but didn't work for me, could you please show me a solution on my code that I posted? Thank you people very much.
There may be a simpler method that I'm not aware of, but the following method should work:
Use NSNotificationCenter to post a notification back to the ViewController containing the UIPopOverController to tell it to dismiss the popover.
First, in ViewController.m viewDidLoad add:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(closePop:) name:#"ClosePopOver" object:nil];
Then add the following method to ViewController.m:
- (void)closePop:(NSNotification *)notification {
[_firstPopover dismissPopoverAnimated:YES];
}
Then in irstPopoverViewController.m:
- (void)closePop {
[[NSNotificationCenter defaultCenter] postNotificationName:#"ClosePopOver" object:nil];
}
That should do the trick.
A delegate is the way to go. I do admit though that I was confused by them at first, but they are quite simple to setup.
In your firstPopoverController.h put this:
#import <UIKit/UIKit.h>
#protocol FirstPopoverDelegate
- (void) closedPopover;
#end
#interface firstPopoverViewController : UIViewController
#property (nonatomic, assign) id< FirstPopoverDelegate > delegate;
#end
Then in your .m of you popover,
-(void)closePop
{
[self.delegate closedPopover];
}
In your main UIViewController's .h:
#import <UIKit/UIKit.h>
#import "firstPopoverViewController.h"
#interface ViewController : UIViewController <FirstPopoverDelegate>
#property (strong, nonatomic) UIButton *popButton;
#property (strong, nonatomic) firstPopoverViewController *firstPopoverViewController;
#property (strong, nonatomic) UIPopoverController *firstPopover;
#end
Then in the .m, first register to listen of the delegate by adding this to your openPop method:
this is important and easy to forget.. nothing will happen if it is not set
_firstPopoverViewController.delegate = self;
Finally, in your .m add the delegate method:
- (void)closedPopover
{
//you can also pass data back in this function, just modify its parameters here and when you define it in the .h of the popover
[_firstPopoverViewController dismissPopoverAnimated:YES];
}
I am trying to apply UITapGestureRecognizer on a UILabel in order to check and open e-mail service. The current UIView is a part of a UIViewController and displayed once user tap on a button.
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#class AddressBook;
#interface ContactInfoUI : UIView <MFMailComposeViewControllerDelegate, UIGestureRecognizerDelegate>{
IBOutlet UIView *view;
UIViewController *mContainerVc;
AddressBook *mAddressBook;
}
#property (nonatomic, retain)UIView *view;
#property (nonatomic, retain)UIViewController *mContainerVc;
#property (nonatomic, retain)AddressBook *mAddressBook;
-(void)addContactInformationFrom:(AddressBook *)addressBook;
#end
.m
#implementation ContactInfoUI
#synthesize view;
#synthesize mContainerVc;
#synthesize mAddressBook;
- (id)init {
self = [super init];
if (self) {
[[NSBundle mainBundle] loadNibNamed:#"ContactInfoView" owner:self options:nil];
self.userInteractionEnabled = YES;
[self addSubview:[self view]];
}
return self;
}
-(void)addContactInformationFrom:(AddressBook *)addressBook{
self.mAddressBook = addressBook;
int y = 20;
CGRect rect = CGRectMake(20, y, 320, 60);
if (![mAddressBook.aEmail isEqualToString:#"-"]) {
UILabel *email = [[UILabel alloc] initWithFrame:rect];
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showEmailForm:)];
tgr.delegate = self;
tgr.numberOfTapsRequired = 1;
[email addGestureRecognizer:tgr];
email.userInteractionEnabled = YES;
email.text = mAddressBook.aEmail;
[self addSubview:email];
rect.origin.y += 40;
}
}
-(IBAction)showEmailForm:(id)sender{
// Email Subject
NSString *emailTitle = #"Test Email";
// Email Content
NSString *messageBody = #"Some message";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:#"test#apple.com"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
[mContainerVc presentViewController:mc animated:YES completion:NULL];
}
#end
VC.h (part)
#class ContactInfoUI;
#class ElementObject;
#interface ElementDetailsViewController : UIViewController{
ElementObject *element;
IBOutlet ContactInfoUI *infoView;
}
#property(nonatomic, retain) ContactInfoUI *infoView;
- (IBAction)showInfo:(id)sender;
#end
VC.m (part)
- (IBAction)showInfo:(id)sender {
if (infoView == nil) {
infoView = [[ContactInfoUI alloc] init];
infoView.userInteractionEnabled = YES;
infoView.mContainerVc = self;
}
AddressBook *ab = element.getElementAddressBook;
[infoView addContactInformationFrom:ab];
[self.view addSubview:infoView];
infoBtn.selected = YES;
sumBtn.selected = NO;
mapBtn.selected = NO;
infoView.hidden = NO;
staticMapScrView.hidden = YES;
summaryView.hidden = YES;
}
The problem is that, even if I can see the actual UILabel on screen, I can't tap on it and the email function never fired.
I'm glad you figured it. Yeah, if a UI element is smaller than any of the its superviews (all the way up the chain), all UI interaction to that element will be blocked. This goes for labels, buttons, etc..