Skip to content

Commit

Permalink
Tab titles are automatically updated now, new demo
Browse files Browse the repository at this point in the history
  • Loading branch information
graetzer committed Jun 16, 2012
1 parent 5168574 commit 8da6f1c
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 59 deletions.
1 change: 0 additions & 1 deletion Demo/SGTabs/SGAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ - (void)openTab {
SGViewController *vc = [[SGViewController alloc]
initWithNibName:NSStringFromClass([SGViewController class])
bundle:nil];
vc.title = [NSString stringWithFormat:@"Tab %i content", self.tabController.count+1];
[self.tabController addTab:vc];
}

Expand Down
5 changes: 3 additions & 2 deletions Demo/SGTabs/SGViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@

#import <UIKit/UIKit.h>

@interface SGViewController : UIViewController
@interface SGViewController : UIViewController <UITextFieldDelegate, UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (strong, nonatomic) UITextField *textField;

@end
76 changes: 59 additions & 17 deletions Demo/SGTabs/SGViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,47 @@ @interface SGViewController ()
@end

@implementation SGViewController
@synthesize label;
@synthesize webView, textField = _textField;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.label.text = self.title;

self.webView.delegate = self;
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 300.0, 25.0)];
self.textField.backgroundColor = [UIColor whiteColor];
self.textField.text = @"http://www.google.com";
self.textField.clearButtonMode = UITextFieldViewModeAlways;
self.textField.delegate = self;
[self textFieldDidEndEditing:self.textField];

UIBarButtonItem *urlBar = [[UIBarButtonItem alloc] initWithCustomView:self.textField];

UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
UIBarButtonItem *trash = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
target:self
action:@selector(remove:)];
UIBarButtonItem *space2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
UIBarButtonItem *reload = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self action:@selector(reload:)];
UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(add:)];
self.toolbarItems = [NSArray arrayWithObjects:space, trash, add, nil];
UIBarButtonItem *trash = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
target:self
action:@selector(remove:)];
SGTabsViewController *tabs = (SGTabsViewController *) self.parentViewController;
if (tabs.count)
self.toolbarItems = [NSArray arrayWithObjects:space,urlBar,space2,trash,reload,add,nil];
else
self.toolbarItems = [NSArray arrayWithObjects:space,urlBar,space2,reload,add,nil];

}

- (void)viewDidUnload
{
[self setLabel:nil];
[self setTextField:nil];
[self setWebView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (void)viewWillDisappear:(BOOL)animated {
NSLog(@"viewWillDisappear");
}

- (void)viewDidDisappear:(BOOL)animated {
NSLog(@"ViewDidDisappear");
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Expand All @@ -69,12 +78,45 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
return YES;
}
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *title = [request.URL absoluteString];
if (![self.title isEqualToString:title]) {
self.title = title;
}
if (navigationType == UIWebViewNavigationTypeLinkClicked)
self.textField.text = title;

return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
NSString *text = [textField.text lowercaseString];
if (![text hasPrefix:@"http://"] && ![text hasPrefix:@"https://"]) {
text = [NSString stringWithFormat:@"http://%@", text];
}

NSURL *url = [NSURL URLWithString:text];
NSURLRequest *request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLCacheStorageAllowedInMemoryOnly
timeoutInterval:10.];
[self.webView loadRequest:request];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}

- (IBAction)remove:(id)sender {
SGTabsViewController *tabs = (SGTabsViewController *) self.parentViewController;
[tabs removeViewController:self];
}

- (IBAction)reload:(id)sender {
[self.webView reload];
}

- (IBAction)add:(id)sender {
SGTabsViewController *tabs = (SGTabsViewController *) self.parentViewController;

Expand Down
77 changes: 42 additions & 35 deletions Demo/SGTabs/en.lproj/SGViewController.xib
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<array key="IBDocument.IntegratedClassDependencies">
<string>IBProxyObject</string>
<string>IBUIView</string>
<string>IBUILabel</string>
<string>IBUIWebView</string>
</array>
<array key="IBDocument.PluginDependencies">
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
Expand All @@ -35,41 +35,26 @@
<reference key="NSNextResponder"/>
<int key="NSvFlags">274</int>
<array class="NSMutableArray" key="NSSubviews">
<object class="IBUILabel" id="638580792">
<object class="IBUIWebView" id="206436388">
<reference key="NSNextResponder" ref="774585933"/>
<int key="NSvFlags">274</int>
<string key="NSFrame">{{77, 159}, {167, 142}}</string>
<string key="NSFrameSize">{320, 460}</string>
<reference key="NSSuperview" ref="774585933"/>
<reference key="NSNextKeyView"/>
<reference key="NSWindow"/>
<string key="NSReuseIdentifierKey">_NS:9</string>
<bool key="IBUIOpaque">NO</bool>
<bool key="IBUIClipsSubviews">YES</bool>
<int key="IBUIContentMode">7</int>
<bool key="IBUIUserInteractionEnabled">NO</bool>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
<string key="IBUIText">Label</string>
<object class="NSColor" key="IBUITextColor">
<object class="NSColor" key="IBUIBackgroundColor">
<int key="NSColorSpace">1</int>
<bytes key="NSRGB">MCAwIDAAA</bytes>
</object>
<nil key="IBUIHighlightedColor"/>
<int key="IBUIBaselineAdjustment">0</int>
<float key="IBUIMinimumFontSize">10</float>
<int key="IBUITextAlignment">1</int>
<object class="IBUIFontDescription" key="IBUIFontDescription">
<int key="type">1</int>
<double key="pointSize">35</double>
</object>
<object class="NSFont" key="IBUIFont">
<string key="NSName">Helvetica</string>
<double key="NSSize">35</double>
<int key="NSfFlags">16</int>
<bytes key="NSRGB">MSAxIDEAA</bytes>
</object>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
<int key="IBUIDataDetectorTypes">1</int>
<bool key="IBUIDetectsPhoneNumbers">YES</bool>
</object>
</array>
<string key="NSFrame">{{0, 20}, {320, 460}}</string>
<reference key="NSSuperview"/>
<reference key="NSNextKeyView" ref="638580792"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="206436388"/>
<object class="NSColor" key="IBUIBackgroundColor">
<int key="NSColorSpace">3</int>
<bytes key="NSWhite">MQA</bytes>
Expand All @@ -91,11 +76,11 @@
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
<string key="label">label</string>
<string key="label">webView</string>
<reference key="source" ref="372490531"/>
<reference key="destination" ref="638580792"/>
<reference key="destination" ref="206436388"/>
</object>
<int key="connectionID">10</int>
<int key="connectionID">16</int>
</object>
</array>
<object class="IBMutableOrderedSet" key="objectRecords">
Expand All @@ -121,13 +106,13 @@
<int key="objectID">6</int>
<reference key="object" ref="774585933"/>
<array class="NSMutableArray" key="children">
<reference ref="638580792"/>
<reference ref="206436388"/>
</array>
<reference key="parent" ref="0"/>
</object>
<object class="IBObjectRecord">
<int key="objectID">9</int>
<reference key="object" ref="638580792"/>
<int key="objectID">15</int>
<reference key="object" ref="206436388"/>
<reference key="parent" ref="774585933"/>
</object>
</array>
Expand All @@ -137,16 +122,38 @@
<string key="-1.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string key="-2.CustomClassName">UIResponder</string>
<string key="-2.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string key="15.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string key="6.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string key="9.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
</dictionary>
<dictionary class="NSMutableDictionary" key="unlocalizedProperties"/>
<nil key="activeLocalization"/>
<dictionary class="NSMutableDictionary" key="localizations"/>
<nil key="sourceID"/>
<int key="maxID">14</int>
<int key="maxID">16</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<array class="NSMutableArray" key="referencedPartialClassDescriptions">
<object class="IBPartialClassDescription">
<string key="className">SGViewController</string>
<string key="superclassName">UIViewController</string>
<object class="NSMutableDictionary" key="outlets">
<string key="NS.key.0">webView</string>
<string key="NS.object.0">UIWebView</string>
</object>
<object class="NSMutableDictionary" key="toOneOutletInfosByName">
<string key="NS.key.0">webView</string>
<object class="IBToOneOutletInfo" key="NS.object.0">
<string key="name">webView</string>
<string key="candidateClassName">UIWebView</string>
</object>
</object>
<object class="IBClassDescriptionSource" key="sourceIdentifier">
<string key="majorKey">IBProjectSource</string>
<string key="minorKey">./Classes/SGViewController.h</string>
</object>
</object>
</array>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes"/>
<int key="IBDocument.localizationMode">0</int>
<string key="IBDocument.TargetRuntimeIdentifier">IBCocoaTouchFramework</string>
<object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDependencyDefaults">
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ Tab component for iOS 5+. You can show your ViewControllers in tabs, it is possi
- Show the UIBarButtonItems in viewController.toolbarItems in the toolbar
- Enables you tobuild mobile Safari style Apps

# Demo
A basic web browser with tabs, in around 120 lines of code.

# Screenshots
## Screenshots

![Multiple Tabs open](https://github.com/graetzer/SGTabs/raw/master/screen.png "Multiple Tabs open")

![Toolbar visible](https://github.com/graetzer/SGTabs/raw/master/screen_Toolbar.png "A Toolbar with one element visible")
![Multiple tabs](https://github.com/graetzer/SGTabs/raw/master/screen.png "A basic browser with visible toolbar")



Expand Down
14 changes: 14 additions & 0 deletions Source/SGTabsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ - (void)addTab:(UIViewController *)viewController {
[self addChildViewController:viewController];
[self.tabContents addObject:viewController];
viewController.view.frame = _contentFrame;
[viewController addObserver:self
forKeyPath:@"title"
options:NSKeyValueObservingOptionNew
context:NULL];

if (_toobarVisible)
[self.toolbar setItems:viewController.toolbarItems animated:YES];
Expand All @@ -147,6 +151,14 @@ - (void)addTab:(UIViewController *)viewController {
}
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"title"]) {
NSUInteger index = [self.tabContents indexOfObject:object];
SGTabView *tab = [self.tabsView.tabs objectAtIndex:index];
[tab setTitle:[object title]];
[tab setNeedsLayout];
}
}

- (void)showViewController:(UIViewController *)viewController index:(NSUInteger)index {
if (viewController == self.currentViewController
Expand Down Expand Up @@ -190,6 +202,8 @@ - (void)removeViewController:(UIViewController *)viewController index:(NSUIntege
}

[self.tabContents removeObjectAtIndex:index];
[viewController removeObserver:self forKeyPath:@"title"];

if (self.tabContents.count == 0) {//View controller was the last one
[viewController willMoveToParentViewController:nil];
_currentViewController = nil;
Expand Down
Binary file modified screen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed screen_Toolbar.png
Binary file not shown.

0 comments on commit 8da6f1c

Please sign in to comment.